Dear Lazyweb, Perl unpack question

Nov 21, 2008 12:53

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?

perl, lazyweb, geek

Leave a comment

Comments 2

A shot in the semi-dark... tcepsa November 21 2008, 21:06:08 UTC
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.

Reply


elfs November 21 2008, 21:15:24 UTC
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.

Reply


Leave a comment

Up