Detect if the computer is little or big endian

P

Patrik Maunus

Hi!

How can I know if my computer is little or big endian with Ruby?
I've searched the web but only got a few C/C++ solutions
 
P

Peter Hickman

Patrik said:
Hi!

How can I know if my computer is little or big endian with Ruby?
I've searched the web but only got a few C/C++ solutions
Perhaps pack a string into an integer and look at the result?

Windows XPPro, Intel

irb(main):002:0> [1,0,0,0].pack("i")
=> "\001\000\000\000"
irb(main):003:0>

Macintosh OS X, Power PC

irb(main):004:0> [1,0,0,0].pack("i")
=> "\000\000\000\001"
irb(main):005:0>
 
T

ts

P> How can I know if my computer is little or big endian with Ruby?

You can test if [1].pack("I") == [1].pack("N")


rex% ruby -rrbconfig -e 'p Config::CONFIG["host"] if [1].pack("N") == [1].pack("I")'
"sparc-sun-solaris2.7"
rex%

moulon% ruby -rrbconfig -e 'p Config::CONFIG["host"] unless [1].pack("N") == [1].pack("I")'
"i686-pc-linux-gnu"
moulon%


Guy Decoux
 
D

Daniel Lewis

Windows XPPro, Intel

irb(main):002:0> [1,0,0,0].pack("i")
=3D> "\001\000\000\000"
irb(main):003:0>

Macintosh OS X, Power PC

irb(main):004:0> [1,0,0,0].pack("i")
=3D> "\000\000\000\001"
irb(main):005:0>

I like this, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top