#include <iostream>
using namespace std;
typedef unsigned char BYTE;
typedef long long LONGLONG;
inline long SwapLong(const BYTE* pByte)
{
return (pByte[0] << 24) |
(pByte[1] << 16) |
(pByte[2] << 8) |
pByte[3];
}
inline LONGLONG SwapI64(const BYTE* pByte)
{
return ((LONGLONG)SwapLong(pByte)) << 32 |
SwapLong(pByte + 4);
}
int main( void )
{
long l = 0x01020304;
LONGLONG ll = 0x0102030405060708;
BYTE * pl = (BYTE*)&l;
BYTE * pll = (BYTE*)≪
cout<<(long)pl[0]<<endl;
cout<<(LONGLONG)pll[0]<<endl;
l = SwapLong(pl);
ll = SwapI64(pll);
cout<<(long)pl[0]<<endl;
cout<<(LONGLONG)pll[0]<<endl;
return 0;
}
本机字节序转网络字节序的一种实现
最新推荐文章于 2023-05-07 11:51:07 发布