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