2.3.3. CPU architecture

Four common macros from Berkeley UNIX are used for doing byte order conversions between different CPU architecture types:

You can use these functions as either macros or functions. They accept 16-bit and 32-bit quantities as shown and convert them from network format (big-endian) to the format supported by the local system.

If your system is using the ARM processor in big-endian mode, these macros can return the variable passed, for example:

#define		htonl(l) (l)
#define		htons(s) (s)
#define		ntohl(l) (l)
#define		ntohs(s)	 (s)

If your system is using the ARM processor in little-endian mode, the byte order must be swapped. For htonl() and ntohl(), use the lswap() function provided in the \armthumb directory (see Sample package directories). For htons() and ntohs(), use a byte-swapping macro, as shown below:

#define		htonl(l) 			lswap(l)
#define		htons(s) 			((u_short)		(((u_short)(s) >> 8) |
					           ((u_short)(s) << 8)))
#define		ntohl(l) 			lswap(l)
#define		ntohs(s)			htons(s)
Copyright © 1998-2001 ARM Limited. All rights reserved.. All rights reserved.ARM DUI 0144B
Non-Confidential