大小端

所谓的大端模式(Big-endian),是指数据的高字节,保存在内存的低地址中,而数据的低字节,保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放;
例子:
0000430: e684 6c4e 0100 1800 53ef 0100 0100 0000
0000440: b484 6c4e 004e ed00 0000 0000 0100 0000
在大端模式下,前32位应该这样读: e6 84 6c 4e ( 假设int占4个字节)
记忆方法: 地址的增长顺序与值的增长顺序相同

小端模式

所谓的小端模式(Little-endian),是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中,这种存储模式将地址的高低和数据位权有效地结合起来,高地址部分权值高,低地址部分权值低,和我们的逻辑方法一致。
例子:
0000430: e684 6c4e 0100 1800 53ef 0100 0100 0000
0000440: b484 6c4e 004e ed00 0000 0000 0100 0000
在小端模式下,前32位应该这样读: 4e 6c 84 e6( 假设int占4个字节)
记忆方法: 地址的增长顺序与值的增长顺序相反

#include<stdio.h>
typedef unsigned char * byte_pointer;
void show_bytes(byte_pointer start,int len)
{
    int i;
    for(i=len-1; i>=0; i--)
    {
        printf("%.2x",start[i]);
    }
    printf("
");
}
void show_int(int x)
{
    show_bytes((byte_pointer)&x,sizeof(int));
}

void show_float(float x)
{
    show_bytes((byte_pointer)&x,sizeof(float));
}

void show_pointer(void *x)
{
    show_bytes((byte_pointer)&x,sizeof(void *));
}

int main(void)
{
    int a;
    int *c;
    c=&a;
    show_pointer(c);
    printf("%x
",&a);
    return 0;
}


原文地址:https://www.cnblogs.com/coded-ream/p/7207984.html