判断大小端 电脑是小端 手机是大端
bool islittle()
{typedef union A{char a;short b;}A;A ua;ua.a = 1;return ua.a ;
}int main()
{if (islittle()){printf("小端");}else{printf("大端");}return 0;
}
将小端转成大端
int LittleToBig(int n)//0x12345678->0x78563412
{typedef union B{char arr[4];int b;}B;B ub;ub.b = n;int len = 4;char tmp = 0;for (int i = 0, j = len - 1; i < len; i++, j--)//因为b的数据将a覆盖 所以a的值交换位置 b的值也交换位置{tmp = ub.arr[i];ub.arr[i] = ub.arr[j];ub.arr[j] = tmp;}return ub.b;
}
int main()
{printf("%x", LittleToBig(0x12345678));return 0;
}
本文链接:https://my.lmcjl.com/post/1768.html
展开阅读全文
4 评论