#include <stdio.h>
#include <iostream>
#include <bitset>
using namespace std;
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;
}
#define sw17(x) \
((short)( \
(short)(((x) & (short)0x00ffU) << 8) | \
(short)(((x) & (short)0xff00U) >> 8) ))
int main(char argc, char* argv[])
{
U32 num = 0xc2fe;
//cout << sw16(num) << endl;
//cout << bitset<16>() << endl;
cout << bitset<16>sw17(num) << endl;
cout << bitset<32>(bigToLittle(num)) << endl;
return 0;
}
大小端转换
于 2023-03-29 15:31:37 首次发布