CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
I need help with "pack" and "unpack"


New User

Feb 12, 2014, 12:38 PM

Post #1 of 3 (1785 views)
I need help with "pack" and "unpack" Can't Post

Can someone shed some light on how to use the "pack" and "unpack" functions to encode and decode binary data?

The following code yields unexpected results. Basically, I'm trying to write a very simple binary to character to decimal converter using pack and unpack, with little success.

my $b = "00000110"; 
print "B before pack = $b\n";
my $v = pack("B8", $b);
print "v after pack = $v\n"; # <== what I expect.

# I get expected results here, basically undoing what I did above.
my $v1 = unpack("B8 ", $v);
$v1 = $v1 || "";
print "V1 after unpack = $v1\n";

# Unpacking the packed value in 'i' (integer) format.
my $n = unpack("i*", $v);
$n = $n || "";
print "n = $n\n";
# Would expect to get 6 here, but don't (?????)

Veteran / Moderator

Feb 12, 2014, 2:00 PM

Post #2 of 3 (1776 views)
Re: [bobt_1234] I need help with "pack" and "unpack" [In reply to] Can't Post

Can you please:
- explain what results you are really expecting for your code;
- explain what your results are;
- specify how the results differ from your expectation.

(OK, I have run your code, I know what the results are, but I would like a synoptical comparison of what you get and what you expected, how and why.)

I am very far from being an expert on the pack/unpack functions (I really don't use them often), but I have used them a few times lately and thus have some (limited) idea on how they work, and, prima facie, I am not so surprised by the results. So, please explain why you expect something else.

Meanwhile, these links might help you it you don't know them already:

Granted, this documentation is not among the best pieces of documentation I have seen recently, but, well, you and I (and others) have to live with that. The other link gives in my view a less detailed but better overview:

New User

Feb 13, 2014, 7:36 AM

Post #3 of 3 (1729 views)
Re: [Laurent_R] I need help with "pack" and "unpack" [In reply to] Can't Post

I've solved the problem!

I had to use the correct descriptor to correctly unpack an 8-bit packed value.

Since "pack" and "unpack" are inverse functions, I expected to get back the original value after running something through pack and unpack. Unpacking using the "i" descriptor should return the value in decimal instead of binary format (in this case, 6 instead of 00001100).

It didn't work because $v is 8 bits (the result of pack using "B8"), and the "i" descriptor wants a 32-bit value. The "c" descriptor is needed to unpack an 8-bit integer.

my $b = "00001100"; # Binary 6. 
my $v = pack("B8", $b);
print "Packed value = $v\n"; # <-- a "spade" or the printable representation of Ascii-6.
my $n = unpack("i*", $v); # <-- Should unpack the value as "6" instead of "0110".
print "Unpacked value as decimal = $n\n"; # <-- but prints blank space instead.

my $nn = unpack("c*", $v); # <-- Unpack, treating $v as 8-bits. Alas this works.
print "Unpacked value is $nn\n"; # <-- Ah, now it prints "6" as I expected ("0110"B ==> "6"D).

Thanks for your time and support.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives