double是8字节的数据类型,因此应该首先有交换8字节整型的能力。
/* 转换8字节整型: */
typedef unsigned long long UINT64;
#define ntohll(x) ( ((UINT64)x & 0xff00000000000000LL)>>56 | /
((UINT64)x & 0x00ff000000000000LL)>>40 | /
((UINT64)x & 0x0000ff0000000000LL)>>24 | /
((UINT64)x & 0x000000ff00000000LL)>>8 | /
((UINT64)x & 0x00000000ff000000LL)<<8 | /
((UINT64)x & 0x0000000000ff0000LL)<<24 | /
((UINT64)x & 0x000000000000ff00LL)<<40 | /
((UINT64)x & 0x00000000000000ffLL)<<56 )
/* 为了使问题清晰,先定义个函数 */
double SwapDoubleEndian(double* pdVal)
{
UINT64 llVal = ntohll(*((UINT64*)pdVal));
return *((double*)&llVal);
}
/* 转换double数据的宏 */
#define ntohd(x) (SwapDoubleEndian(&(x)))
float字节序亦可仿此
关键字:double endian float ntohll