Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How can I programatically determine the Endian-ness of the CPU?

by ton (Friar)
on Jan 26, 2002 at 05:02 UTC ( [id://141702]=perlquestion: print w/replies, xml ) Need Help??

ton has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I've written a Blowfish encyption module using Inline::C, and had pretty good results with it. I am currently using this module to encrypt data streams sent from one computer to another. However, I've run into a problem in transmitting data between machines with different architectures: the endien-ness of the CPU affects the encryption results. I got around this through a _ByteReverse function that is used if some compile-time flag is set, and I set that compile time flag ($bigEndian) within the BEGIN section of the package. So far, so good.

My problem lies in determining whether or not $bigEndian should be true. I know I can check $^O for the OS type, and this lets me cheat a bit: Windows and Linux systems are usually Little-Endian, while HPUX is usually Big-Endian. But sparc machines can be either, and Linux _could_ be installed on a machine not using an Intel or AMD CPU.

Is there some perl function that can give me the endian-ness of the CPU? Or some subroutine I can write to have the package determine this on its own? Or am I stuck with my OS-type hack?

Any help is appreciated,

-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...

  • Comment on How can I programatically determine the Endian-ness of the CPU?

Replies are listed 'Best First'.
Re: How can I programatically determine the Endian-ness of the CPU?
by grep (Monsignor) on Jan 26, 2002 at 05:07 UTC
    Use the Config module and $Config{byteorder}.

    Straight from module docs:
    byteorder From byteorder.U: This variable holds the byte order. In the following, larger digits indicate more significance. The variable byteorder is either 4321 on a big-endian machine, or 1234 on a little-endian, or 87654321 on a Cray +... or 3412 with weird order !


    BTW doing a search of determine endian yeilded several results.

    search is your friend

    grep
    grep> cd pub
    grep> more beer
Re: How can I programatically determine the Endian-ness of the CPU?
by wog (Curate) on Jan 26, 2002 at 05:14 UTC
    In addition to checking Config as suggested by grep, you can use a solution provided by perlport. To quote:
    If you need to distinguish between endian architectures you could use either of the variables set like so:
    $is_big_endian = unpack("h*", pack("s", 1)) =~ /01/; $is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://141702]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found