大小端问题

来源:互联网 发布:北京奥运会圣火 知乎 编辑:程序博客网 时间:2024/05/02 02:47

小端字节序

数据 (由小到大)                    地址(由低到高)

0x0029f458                                0x78

0x0029f459                         0x56

0x0029f45a       0x34

0x0029f45b       0x12

大端字节序

数据(由小到大)                 地址(由高到低)

0x0029f458                            0x12

0x0029f459                            0x34

0x0029f45a                           0x56

0x0029f45b                           0x79

程序判读大小字节序

#include <stdio.h>


typedef union{
unsigned short value;
unsigned char bytes[2];
}Test;


int main(void)
{
    Test test_value;
    test_value.value = 0x1234;


    if(test_value.bytes[0] == 0x12 && test_value.bytes[1] == 0x34)
    printf("big ending");
    else if(test_value.bytes[0] == 0x34 && test_value.bytes[1] == 0x12)
    printf("little ending");
    else
    printf("use test_value error");
    return 0;
}

{

union{short value; char union_bytes[sizeof(short)] } test;

test.value = 0x0102;




0 0
原创粉丝点击