大端小端简单测试

时间:2022-12-13 16:00:18


//bigdien or
DWORD wd = 0x22; //unsigned long 32位机器上是4个字节
if( *((BYTE *)&wd) == 0x22 ) //BYTE unsigned char 1个字节
AfxMessageBox(_T("Small-Endian"));
else
AfxMessageBox(_T("Big-Endian"));



这是VC中的测试。。。




例外一个完整的C程序如下:


/*
* file: endian.c
* descriptio: test our machine is big-endian or small-endian
*/

#include <stdio.h>

int main(int argc, char *argv[])
{
union tagwt{
int a;
short b;
}wt;

wt.a = 0x11223344;

//sizeof(wt)=4,sizeof(int)=4,sizeof(short)=2
printf("sizeof(wt)=%d,sizeof(int)=%d,sizeof(short)=%d\n", sizeof(wt), sizeof(int), sizeof(short));

if (wt.b == 0x3344)
printf("small-endian\n");
else if(wt.b == 0x1122)
printf("big-endian\n");
else
printf("what's the fucking!");
}