A quick way to determine endianness

One way is to write a C program that goes:

#include <stdio.h>
int main()
{
   unsigned int i = 1;
   char *c = (char*)&i;
   if (*c)
       printf("Little endian");
   else
       printf("Big endian");
   return 0;
}

Another way is to run a one-liner in your terminal. It goes:

[ "$(echo hi | iconv -t utf-16 | od -N2 -An -t x1 | tr -d ' ')" = "fffe" ] && echo "Little Endian" || echo "Big Endian"

It takes advantage of the magic number 0xFEFF that UTF-16 encoding adds before any text. Look here for more info.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s