大小端转换

关键字:大端  小端  主机字节序  网络字节序

 

/** * @file Code_Convert.h * @brief * @author Donhao * @date 2010-7-17 12:33:16 * @version *

copyright: 
*
All rights reserved.
*
modification:
*
Write modifications here.
*/ #ifndef _CODE_CONVERT_H #define _CODE_CONVERT_H /** * @defgroup 主机字节序与网络字节序之间的转换 * @brief * * Detailed description. * @{ */ /** * @brief Code_Is_Net * * 判断本机是主机字节序还是网络字节序. * @return BOOL */ BOOL Code_Is_Net(void); /** * @brief Code_H2N16 * * @param[in] x * @return unsigned short */ unsigned short Code_H2N16(unsigned short x); /** * @brief Code_H2N32 * * @param[in] x * @return unsigned long */ unsigned long Code_H2N32(unsigned long x); /** @} */ #endif

 

/** * @file Code_Convert.c * @brief * @author Donhao * @date 2010-7-17 12:33:31 * @version *

All rights reserved.
*
modification:
*
Write modifications here.
*/ #include "Code_Convert.h" BOOL Code_Is_Net(void) { unsigned short value = 0x55AA; if (0x55 == *((unsigned char*)(&value))) { return TRUE; } else { return FALSE; } } unsigned short Code_H2N16(unsigned short x) { unsigned short y = 0; unsigned short z = 0; z = x & 0x00FF; y = z << 8; z = x & 0xFF00; y += (z >> 8); return y; } unsigned long Code_H2N32(unsigned long x) { unsigned long y = 0; unsigned long z = 0; z = x & 0x000000FF; y = z << 24; z = x & 0x0000FF00; y += (z << 8); z = x & 0x00FF0000; y += (z >> 8); z = x & 0xFF000000; y += (z >> 24); return y; }

你可能感兴趣的:(C,C++)