How to determine the Endianness of your PC

While studying networks I came across two system calls namely

  1. ntoh(s/l)
  2. hton(s/l)

Both of the above are related to byte ordering done in the network layer

ntoh(s/l)

It is used by host to convert a packet from network byte order to host byte order. (s for short and l for long).

hton(s/l)

It is used by host before sending a packet to the network. This function converts a packet in host byte order to network byte order. (s for short and l for long).

I am not writing the example syntaxes of the above functions because you can easilly get them in the internet.

What will happen if I do not use the above functions?

Well this depends upon the endianess of your machine. If your machine follows Big Endian then you can do away with the functions, but if your pc follows little endian then you may get erroneous results. This is because almost all network protocols follow big endian.

How to check endianness of your pc

  • You can either use the following command
lscpu

and check the endianness of your pc

  • You can also use the following cpp code to check the endianness of your pc
# include <stdio.h>
int main(void)
{
   unsigned long i = 1;
   int *c = (int*)&i;
   if (c[0]==1)    
       printf("Little endian\n");
   else if(c[1]==1)
       printf("Big endian\n");
   else
    printf("Unknown Endianness\n");

}
Anurag Roy
Anurag Roy
PhD in Continual Machine Learning

My research interests include using of multimodal machine learning to analyze, recognize and predict human behaviour.

comments powered by Disqus

Related