http://www.perlmonks.org?node_id=102506

I am playing around with raw sockets and manufactured packets in pure-perl (no Net::Pcap etc). Along the way I figured I needed to know the whether the machine was big-endian (network order) or little-endian.

Actually, I don't need to know this, but here is the code for detecting endianness for normal big and little 32bit machines. Extensions for 64bit welcome; but this works for me on Linux-IA32 and Solaris2.6-UltraSparc .

@b = unpack('CCCC', pack('I', 0x04030201)); if ($b[0] == 4 and $b[1] == 3 and $b[2] == 2 and $b[3] == 1 ) { print "big-endian\n"; } elsif ($b[0] == 1 and $b[1] == 2 and $b[2] == 3 and $b[3] == 4 ) { print "little-endian\n"; } else { print "funky-endian\n"; }