Little Andian - The Big End Challenge

Little Indian vs Big Indian

Big Endian = 0x31014950
Little Endian = 0x50490131

However using this method

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

result = 0x54110131
I spent a lot of time trying many similar methods and even a library like

unsigned long _byteswap_ulong(unsigned long value);  

But still no luck .. everything returns the same result

EDIT
I'm working on a Little-Endian system with Microsoft Visual Studio 2008
example, as follows

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

the example posted on Ideone.com is correct, but it does not work with me

EDIT

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

Wails Pals.. Hex Not it not get the Right Number.. Visual Studio , .

Redunant ,
BTW, . Debugger , .

+4
7
  • ?
  • , , ?
+2

.

(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


Edit:
std::cout << std::hex << r, (1) (2) : -)

: http://ideone.com/EPFz8

+2

x . .

+1

?

:

void LittleEndianTest::testLittleEndian()
{
    unsigned int x = 0x31014950;
    unsigned int result = 
         (
             ( (x & 0x000000FF) << 24 ) + 
             ( (x & 0x0000FF00) << 8  ) +
             ( (x & 0x00FF0000) >> 8  ) +
             ( (x & 0xFF000000) >> 24 ) 
         );

    CPPUNIT_ASSERT_EQUAL((unsigned int)0x50490131, result); 
}
0

y r. y, , .

0

, , : endian_swap ( 0x31014950 ) == 0x50490131.

: endian_swap ( 0x31014950 ) == 0x54110131, :

#define __
inline unsigned int endian_swap(unsigned int& x)  
{                  //0x31014950  ->            0x54110131
    return  ( ( (x & 0x000000FF) << 24 ) |   //  50
        __    ( (x & 0x00F0F000) << 12 ) |   //   4
        __    ( (x & 0x00FF0000) << 4  ) |   //    1  
        __    ( (x & 0x00FF0000) << 0  ) |   //     1  
        __    ( (x & 0x00FF0000) >> 8  ) |   //      01
        __    ( (x & 0xFF000000) >> 24 ) );  //        31        
}

, .

0
source

Bit operators are not useful because they work as if the bits are in order from the least significant bit to the most significant bit, regardless of the true internal byte order.

void isBigEndian()
{
    void *number;
    number = (int *) new int(0x01000010);
    // 0x01000010
    //   01 00 00 10                                      Hexadecimal
    //   0    1      0    0      0    0       1    0
    //   0000 0001   0000 0000   0000 0000    0001 0000   Bit
    //   1           0           0            16          Decimal
    char *byte;
    byte = (char *)number;
    cout << static_cast<int>(*byte);//prints 16: Little Endian
}

You change the number above and make it return 1 when it is BigEndian.

0
source

All Articles