更新时间:2023-11-19 22:30:22
您的代码似乎是正确的。
Your code seems to be correct.
以下程序( http://ideone.com/a5TBF ) :
#include <cstdio>
inline unsigned int endian_swap(unsigned const int& x)
{
return ( ( (x & 0x000000FF) << 24 ) |
( (x & 0x0000FF00) << 8 ) |
( (x & 0x00FF0000) >> 8 ) |
( (x & 0xFF000000) >> 24 ) );
}
int main()
{
unsigned int x = 0x12345678;
unsigned int y = endian_swap(x);
printf("%x %x\n", x, y);
return 0;
}
输出:
12345678 78563412
12345678 78563412
编辑:
你需要 std :: cout<< std :: hex<< r
you need std::cout << std::hex << r
, otherwise you are printing (1) wrong variable, and (2) in decimal :-)
查看此示例: http://ideone.com/EPFz8