Dear Monks,
I have the following script which I ran on 2 different machines:
#!/usr/local/bin/perl
#
use strict;
use warnings;
use Config;
print "Byte order: $Config{byteorder}\n"; # expect byteorder='43
+21' or '1234';
my $hexstring = "01020304"; # Hex number
my $decnumber = hex $hexstring;
print "Hex: $hexstring is Dec: $decnumber\n"; # expect 1690
+9060
my $netnumber = pack( "N", $decnumber );
print "Dec: $decnumber is 'N': '",unpack("H8",$netnumber),"' as Ne
+twork or Big-endian\n";
my $Nonnetnumber = pack( "V", $decnumber ); # Vax or little-e
+ndian
print "Dec: $decnumber is 'V': '",unpack("H8",$Nonnetnumber),"' as
+ Vax or Little-endian\n";
$Nonnetnumber = pack( "L", $decnumber ); # as native format
print "Dec: $decnumber is 'L': '",unpack("H8",$Nonnetnumber),"' as
+ native format\n";
The results were:
On Aix 5.2 on RS/6000 ( powerpc-ibm-aix5.2.0.0 )
# pyrperl ./pack.pl
Byte order: 4321
Hex: 01020304 is Dec: 16909060
Dec: 16909060 is 'N': '01020304' as Network or Big-endian
Dec: 16909060 is 'V': '04030201' as Vax or Little-endian
Dec: 16909060 is 'L': '01020304' as native format
On Debian Linux on 64bit AMD ( Debian 4.4.5-8 )
# pyrperl ./pack.pl
Byte order: 1234
Hex: 01020304 is Dec: 16909060
Dec: 16909060 is 'N': '01020304' as Network or Big-endian
Dec: 16909060 is 'V': '04030201' as Vax or Little-endian
Dec: 16909060 is 'L': '04030201' as native format
The results agree with
Writing endian-independent code in C. My knowledge of big-endian came from writing machine to machine transfer software for IBM in the '70s.
The background was that IBM 32-bit mainframes were big-endian and expensive, and the new in-expensive (???) 8-bit boxes were little-endian.
As you can see the 'N' and 'V' 'pack' formats are the same on both architectures but the native formats are different. Perl's 'pack' is correct, but the documentation examples are incorrect.
Regards...Ed
"Well done is better than well said." - Benjamin Franklin
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.