fallenpegasus: amazon (Default)
Mark Atwood ([personal profile] fallenpegasus) wrote2008-11-21 12:53 pm
Entry tags:

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?

A shot in the semi-dark...

[identity profile] tcepsa.livejournal.com 2008-11-21 09:06 pm (UTC)(link)
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 2008-11-21 21:10 (UTC)

[identity profile] elfs.livejournal.com 2008-11-21 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.

achat cialis 411

(Anonymous) 2013-07-13 06:13 pm (UTC)(link)
cialis en ligne sans ordonnance (http://achetercialisici.com/#onhgj) - acheter cialis (http://achetercialisici.com/#tiyns) , http://achetercialisici.com/#rhvug acheter cialis 20 mg