Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BigEndian to LittleEndian conversion

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
I am looking for a function that will convert an argument(either 16 bit, 32
bit, ... basically a BIGENDIAN) to a LITTLE ENDIAN. The argument can be a
pointer to some memory address which will hold the arg. value.

This function should be intrinsic and lightweight.

 
byteswap(char* ptr, int size)
{
[ignore]
for(int i = 0, j = size-1;i<j;i++,j--)
{
char temp = ptr;
ptr = ptr[j];
ptr[j] = ptr;
}
}

That should do it... to call


byteswap(type, sizeof(type))

Matt

 
Sorry... typo

byteswap(char* ptr, int size)
{
for(int i = 0, j = size-1;i<j;i++,j--)
{
char temp = ptr[ignore][/ignore];
ptr[ignore][/ignore] = ptr[j];
ptr[j] = temp;
}
}

That should do it... to call


byteswap(type, sizeof(type))

Matt

 
you can use ANSI swab function, defined as:
Code:
 void swab(const void * srt,void *dest,size_t nbytes);
Nosferatu
We are what we eat...
There's no such thing as free meal...
 
There is the net functions

#include <sys/types.h>
#include <netinet/in.h>


ulong htonl(ulong hostlong)
ulong ntohl(ulong netlong)
ushort ntohs(ushort netshort)
ushort htons(ushort hostshort)


These convert between network and host byte
order for 32 & 16 bit quantities respectively.


Since the TCP/IP protocol uses big-endian byte order for network addresses, htonl effectively converts from little
endian to big-endian if your host network order is little-endian.

Regards

Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top