C语言–大小端转换
#include <stdio.h>
typedef unsigned char U8;
typedef unsigned int U32;
U32 bigToLittle(U32 value){
U32 temp = 0;
//方式1
// ((U8*)&temp)[0] = ((U8*)&value)[3];//指针-->数组
// ((U8*)&temp)[1] = ((U8*)&value)[2];
// ((U8*)&temp)[2] = ((U8*)&value)[1];
// ((U8*)&temp)[3] = ((U8*)&value)[0];
//方式2
*((U8*)&temp+0) = *((U8*)&value+3);//指针
*((U8*)&temp+1) = *((U8*)&value+2);
*((U8*)&temp+2) = *((U8*)&value+1);
*((U8*)&temp+3) = *((U8*)&value+0);
printf("%x\n",((U8*)&value)[3]);
return temp;
}
int main(char argc,char* argv[])
{
U32 num = 0x12345678;
printf("%x\n",bigToLittle(num));
return 0;
}