全部博文(25)
分类: C/C++
2008-09-08 22:09:14
你也可以用下面的程序测验你的机器是大端字节序还是小端字节序:
----------------------------------------------------------
#include
int IsLittleEndian()
{
unsigned int usData = 0x12345678;
unsigned char *pucData = (unsigned char*)&usData;
if(*pucData == 0x78)
return 1;
else
return 0;
}
int main(void)
{
if(IsLittleEndian())
printf("is little endian!\n");
else
printf("is big endian!\n");
return 0;
}
a=0x12345678
----------------------------------------------------------
Byte3 Byte2 Byte1 Byte0
will be arranged in memory as follows:
Base Address+0 Byte0 78h
Base Address+1 Byte1 56h
Base Address+2 Byte2 34h
Base Address+3 Byte3 12h
Intel processors (those used in PC's) use "Little Endian" byte order.
"Big Endian" means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address. (The big end comes first.) Our LongInt, would then be stored as:
Base Address+0 Byte3 --12h
Base Address+1 Byte2 34h
Base Address+2 Byte1 56h
Base Address+3 Byte0 78h
Motorola processors (those used in Mac's) use "Big Endian" byte order
Intel的X86体系结构是Little Endian
----------------------------------------------------------
#include
int main()
{
int i = 0x11223344;
int j = 0;
char * a = (char *)&i;
printf("&a[0] = %p\n", &a[0]);
printf("&a[3] = %p\n", &a[3]);
for (j = 0; j < 4; j++)
printf("%x\n", a[j]);
}
--------------------------------
&a[0] = 0xbf82d048
&a[3] = 0xbf82d04b
44
33
22
11
|----------|
| 11 |
| 22 |
|----------| 0xbf82d04a
| 33 |
|----------| 0xbf82d049
| 44 |
|----------|<-- a 0xbf82d048
0x11223344
分别填上:68,67,66,65,65,66,67,68就行,哈哈完事