本文介绍了小端 - 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Little Endian vs Big Endian

Little Endian vs Big Endian

Big Endian = 0x31014950

Little Endian = 0x50490131

Big Endian = 0x31014950
Little Endian = 0x50490131

但是使用此方法

inline unsigned int endian_swap(unsigned int& x)  
{
return ( ( (x & 0x000000FF) << 24 ) | 
         ( (x & 0x0000FF00) << 8  ) |
         ( (x & 0x00FF0000) >> 8  ) |
         ( (x & 0xFF000000) >> 24 ) );
}

result = 0x54110131

i花了很多时间类似的方法,甚至是一个类似

result = 0x54110131
i spent lot of time trying lots of similar methods and even a library one like

unsigned long _byteswap_ulong(unsigned long value);  

但仍然没有运气..所有返回相同的结果

But Still no luck .. all returns same result

EDIT

我正在使用Microsoft Visual Studio 2008处理Little-Endian系统

示例如下

EDIT
I'm Working on Little-Endian System with Microsoft Visual Studio 2008
the example as Follows

int main()
{
    unsigned int y = 0x31014950;
    unsigned int r = endian_swap( y );
    std::cout << r;
}  

在Ideone.com上发布的示例是正确的..但它不与我合作

the example posted on Ideone.com is correct .. but it doesn't work with me

编辑

std::cout << std::hex << r;  

任何一种方法Pals ..十六进制或不是它没有得到正确的数字。严重的错误是它的调试器或我的整个机器。

因为我用更慢的Redunant代码替换了整个代码,但仍然得到相同的结果

如果它有什么区别..我使用Debugger Break后的函数检查结果

Either Ways Pals .. Hex or Not it's not getting the Right Number .. Either Visual Studio got a serious Error in it's Debugger or My Whole Machine ..
Because i Replaced the Whole Code with a More Slow Redunant code but still getting same results
BTW if it makes any difference .. i'm using Debugger to Break after the function to check the result

推荐答案

您的代码似乎是正确的。

Your code seems to be correct.

以下程序() :

#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






编辑:

你需要 std :: cout<< std :: hex<< r



you need std::cout << std::hex << r, otherwise you are printing (1) wrong variable, and (2) in decimal :-)

查看此示例:

这篇关于小端 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:28