Little, big endianess explained — part2
Related Blog Items
- Finding endianness
- Binary Streams Utility Classes
- Structure padding explained
- c/c++ blogging - January 10, 2007
- Buffer Overruns (on Mobile Applications) - Part2
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 - Finding endianness
- Binary Streams Utility Classes
- Structure padding explained
- c/c++ blogging - January 10, 2007
- Buffer Overruns (on Mobile Applications) - Part2
Related Blog Items
- Finding endianness
- Binary Streams Utility Classes
- Structure padding explained
- c/c++ blogging - January 10, 2007
- Buffer Overruns (on Mobile Applications) - Part2
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.
December 28th, 2006 at 2:23 pm
i thnik you mean “if (!IsLittleEndian()). Please replace with if (IsBigEndian()).” not “(!IsBigEndian())”
January 2nd, 2007 at 1:37 pm
[…] ->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) […]
January 9th, 2007 at 1:58 pm
[…] 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? […]
January 18th, 2007 at 3:25 pm
[…] to know about endianness, please refer to my earlier posts… Little, Big endianess explained Little, big endianess explained - part2 […]
April 15th, 2007 at 6:08 pm