70,021
社区成员




long li=0x01020304;
long res=0; //反转后的结果
for(int i=0;i<4;i++)
{
res=res*256+((li>>(i*8))&0xff);
}
//如果是将int a=0xAABBCCDD 转化成 a=0xDDCCBBAA
int change_order(int value) //参数为需要转换的数据。
{
int i=4; //32为系统的int类型的长度。
char temp[4], *p=(char *)&value; //强制类型转换
while(i--)
temp[i]=*p++;
return *((int *)temp); //返回值位转换后的数据
}
//如果是2字节长度的数据则只需要将i的初始值和temp数组的长度同时改为2即可。
/* file name : test_reverse.c
* function : to reverse high data to low data
*/
#include "stdio.h" //printf()
int main(void)
{
unsigned int test_int_a = 0xabcdef98;
unsigned int test_int_b;
unsigned int temp_data = 0;
test_int_b = (test_int_a & 0xffff);
test_int_b <<= 16;
temp_data = test_int_a;
temp_data >>= 16;
test_int_b += temp_data;
return 0;
}
#if 0
int main(void)
{
unsigned int test_int_a = 0xabcdef98;
unsigned int test_int_b;
unsigned int temp_data = 0;
test_int_b = (test_int_a & 0xffff);
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);
test_int_b <<= 16;
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);
temp_data = test_int_a;
temp_data >>= 16;
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);
test_int_b += temp_data;
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);
return 0;
}
a = abcdef98 b = ef98 t = 0
a = abcdef98 b = ef980000 t = 0
a = abcdef98 b = ef980000 t = abcd
a = abcdef98 b = ef98abcd t = abcd
#endif