Unix Incompatibility Notes: Byte Order
Some processors (PowerPC, MIPS, DEC Alpha) can be either big-endian or little-endian depending on software settings.
Network byte order is the standard used in packets sent over the internet. It is big-endian (except that technically it refers to the order in which bytes are transmitted, not the order in which they are stored). If you are going to chose an arbitrary order to standardize on, network-byte order is a sensible choice.
The unix functions htonl(), htons(), ntohl(), and ntohs() convert longs and shorts back and forth between the host byte order and network byte order. However, though they are widely available, they are not universally available.
run time test:
int am_big_endian()
{
long one= 1;
return !(*((char *)(&one)));
//if *(char *)(&one) is 1, means the 1 is store is stored in the first byte, then it is little endian;
//if *(char *)(&one) is 0, means the 1 is store is not stored in the first byte, then it is big endian;
}

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home