How to check system endianness using Ruby
July 2, 2015

Check system endianness using Ruby

Hello folks,

today I’d like to share with you a simple way to check the endianness of your system using Ruby and it’s based on the “pack” method in Ruby’s Array; pack simply represents the content of an array in a binary sequence according to a directive given to it as an argument.

Using this method mades the endianness verification as simple as writing this snippet:

if [1].pack("I") == [1].pack("N") 
 puts "BigEndian" 
 puts "LittleEndian"

The snippet verifies if the array [1] represented using native endianness (the “I” directive) is equal to the same array represented using network (big endian) endianness  ( the “N” directive).

If the result is true the examined system is big endian, otherwise it is a little endian one.


Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.