Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

27
Dec

Little, big endianess explained — part2

Related Blog Items

Byte order utililities

Previous post little, big endianness explained deals with theoretical part of endianness, now let us write some utility functions for endainness.

Little Endian To Host: Data we are taking here is little endian data and this program converts little endian to host format. program takes a buffer of data and rearranges it to the native order of the machine. if the native order is little endian no conversion happens.

IsLittleEndian function is already listed in previous post, as it is used here, we?ve copied that function?


bool IsLittleEndian()
{
int x=1 ;
if(*(char *) &x == 1)
return true;
else
return false;
}

int LittleEndianToNative(char *data, unsigned int size)
{
char tmp;
char *sptr, *eptr;

if (!data || !size)
{
return -1;
}

if (IsLittleEndian())
{
return 0;
}

sptr = data;
eptr = data + size - 1;

while (sptr <= eptr-1)
  {
    tmp  = *sptr;
    *sptr++ = *eptr;
    *eptr– = tmp;
  }
  return 0;
}

Host to Little Endian:

Data we are taking here is host or native order data and this program converts host to little endian format. program takes a buffer of data and rearranges it to little endian. if the native order is little endian no conversion happens.

int NativeToLittleEndian(char *data, unsigned int size)
{
//conversion process is same
return LittleEndianToNative(data, size);
}

Big Endian To Host:

Data we are taking here is big endian data and this program converts big endian to native order format. program takes a buffer of data and rearranges it to the native order of the machine. if the native order is big endian no conversion happens.


int BigEndianToNative(char *data, unsigned int size)
{
char tmp;
char *sptr, *eptr;

if (!data || !size) { return -1; }

if (!IsLittleEndian())
{
//no conversion required
return 0;
}
sptr = data;
eptr = data + size - 1;

while (sptr <= eptr-1)
  {
    tmp  = *sptr;
    *sptr++ = *eptr;
    *eptr– = tmp;
  }
  return 0;
}

Host to Big Endian:

Data we are taking here is host or native order data and this program converts host to big endian format. program takes a buffer of data and rearranges it to big endian. if the native order is big endian no conversion happens.

int NativeToBigEndian(char *data, unsigned int size)
{
//conversion process is same
return BigEndianToNative(data, size);
}

Popularity: 11%

You need to log on to convert this article into PDF


Related Blog Items

5 Comments

  • Sridhar  said:

    Inside the function BigEndianToNative(),

    it should not be
    if (!IsLittleEndian()). Please replace with if (!IsBigEndian()).

    it can be understood, but better to correct.

    ….Sridhar … Needless to mention ….A wizard.


  • ponnada  said:

    i thnik you mean “if (!IsLittleEndian()). Please replace with if (IsBigEndian()).” not “(!IsBigEndian())”


  • Structure padding explained  said:

    […] ->to format a data structure to transmit over network using some protocol (this is not a good practice of course because you need to deal with endianness) […]


  • Binary Streams Utility Classes  said:

    […] Binary streams are commonly used in many applications. They require special care while dealing with them. Here is a binary class which can be deployed and customized according to your need. This binary class has awareness of endian. It act according to endain. If your machine is little endian and you wish to process your binary stream in little endian, then set the mode to little endian. If your machine big endian set the mode to big endian. However little more intelligence yet to be incorporated in case of your machine is little endian and you want the binary stream processing in big endian, in this case you need to use utilities mentioned in the post ?Little and big endianess explained ? part2? […]


  • Finding endianness  said:

    […] to know about endianness, please refer to my earlier posts… Little, Big endianess explained Little, big endianess explained - part2 […]


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image