?

Log in

No account? Create an account
entries friends calendar profile My Website Previous Previous Next Next
Dear Lazyweb, Perl unpack question - Mark Atwood
fallenpegasus
fallenpegasus
Dear Lazyweb, Perl unpack question
So I have a string that has a binary data structure in it, and I want to use Perl's unpack() on it

The binary structure is a 32bit int, followed by N 8 byte chunks. And I don't have quadwords in this build of Perl, so I cant treat them as 64bit ints.

And the following unpack string doesnt work: "L(a8)*".

What would the correct unpack string be?

Tags: , ,

2 comments or Leave a comment
Comments
tcepsa From: tcepsa Date: November 21st, 2008 09:06 pm (UTC) (Link)

A shot in the semi-dark...

What about "L(c8)*" ?

Reasoning: Looks like "a" signifies a null-terminated character string, so your expression would be "A long, followed by some number of groups of 8 null-terminated strings at a time". At the same time, c signifies a signed char (it doesn't sound like signed-ness makes a difference in this particular case), which is a byte, and you want an unknown number of groups of 8 bytes at a time.

Edited to add reasoning and fix the signed-ness of what c actually represents.

Edited at 2008-11-21 09:10 pm (UTC)
elfs From: elfs Date: November 21st, 2008 09:15 pm (UTC) (Link)
Assuming your string is in $c:

use Encode;
$l = (length(Encode::encode_utf8($c)) - 4) / 8;
@f = unpack('L' . ('(a8)' x $l), $c);
print "@f\n";


The Encode forces length to treat your strings as a collection of 8-bit bytes. The 'x' operator (yes, that's really a lower-case x, by the uncaring stars I hate Perl) in a string context causes multiplication of the string entity.
2 comments or Leave a comment