GrandFather has asked for the wisdom of the Perl Monks concerning the following question:
I have some code that is pulling appart a file and is using unpack with a 'n' conversion to extract what should be a 16 bit signed big endien number. I'm getting an unsigned result. Is that expected behaviour?
The following code demonstrates the issue:
use strict; use warnings; print "Network (16 bits) pack/unpack: ", unpack ('n', pack ('n', -1)), + "\n"; print "Short pack/unpack: ", unpack ('s', pack ('s', -1)), "\n"; print "Network (32 bits) pack/unpack: ", unpack ('N', pack ('N', -1)), + "\n"; print "Long pack/unpack: ", unpack ('l', pack ('l', -1)), "\n"; print "Network (cvt) pack/unpack: ", unpack ('s', pack ('s', unpack (' +n', pack ('n', -1)))), "\n";
Prints:
Network (16 bits) pack/unpack: 65535 Short pack/unpack: -1 Network (32 bits) pack/unpack: 4294967295 Long pack/unpack: -1 Network (cvt) pack/unpack: -1
This is using AS Perl 5.8.7 (813) on XP.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unexpected Network conversion behaviour for unpack
by Corion (Patriarch) on Feb 09, 2006 at 23:12 UTC | |
by bart (Canon) on Feb 10, 2006 at 01:16 UTC | |
by GrandFather (Saint) on Feb 09, 2006 at 23:23 UTC |