Perl pack/unpack and length of binary string

Consider this short example:

$a = pack("d",255);
print length($a)."\n";
# prints 8

$aa = pack("ddddd", 255,123,0,45,123);
print length($aa)."\n";
# prints 40

@unparray = unpack("d "x5, $aa);
print scalar(@unparray)."\n";
# prints 5

print length($unparray[0])."\n" 
# prints 3

printf "%d\n", $unparray[0] '
# prints 255

# as one liner:
# perl -e '$a = pack("d",255); print length($a)."\n"; $aa = pack("dd", 255,123,0,45,123); print length($aa)."\n"; @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; print length($unparray[0])."\n"; printf "%d\n", $unparray[0] '

Now, I'd expect a double-precision float to be 8 bytes, so the first length($a) is correct. But why is the length after the unpack (length($unparray[0])) reporting 3 - when I'm trying to go back the exact same way (double-precision, i.e. 8 bytes) - and the value of the item (255) is correctly preserved?

Many thanks in advance for any answers, Cheers!


By unpacking what you packed, you've gotten back the original values, and the first value is 255. The stringification of 255 is "255", which is 3 characters long, and that's what length tells you.

Need Your Help

Symfony2 double db connection with unpredictable parameters

php orm symfony2 doctrine

I'm having some troubles in using Doctrine ORM in my Symfony2 project. Basically i have one central db connection from which i load user data, and among these data the parameters for another db

Why are online and offline events not firing in Firefox?

javascript jquery firefox javascript-events

I am attempting to implement a JS function to get invoked whenever there is a connectivity change, another-words, when the internet connection is lost or regained.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.