Convert Little Endian to Big Endian

前端 未结 13 1861
慢半拍i
慢半拍i 2020-11-27 16:34

I just want to ask if my method is correct to convert from little endian to big endian, just to make sure if I understand the difference.

I have a number which is st

13条回答
  •  不知归路
    2020-11-27 16:43

    OP's sample code is incorrect.

    Endian conversion works at the bit and 8-bit byte level. Most endian issues deal with the byte level. OP code is doing a endian change at the 4-bit nibble level. Recommend instead:

    // Swap endian (big to little) or (little to big)
    uint32_t num = 9;
    uint32_t b0,b1,b2,b3;
    uint32_t res;
    
    b0 = (num & 0x000000ff) << 24u;
    b1 = (num & 0x0000ff00) << 8u;
    b2 = (num & 0x00ff0000) >> 8u;
    b3 = (num & 0xff000000) >> 24u;
    
    res = b0 | b1 | b2 | b3;
    
    printf("%" PRIX32 "\n", res);
    

    If performance is truly important, the particular processor would need to be known. Otherwise, leave it to the compiler.

    [Edit] OP added a comment that changes things.
    "32bit numerical value represented by the hexadecimal representation (st uv wx yz) shall be recorded in a four-byte field as (st uv wx yz)."

    It appears in this case, the endian of the 32-bit number is unknown and the result needs to be store in memory in little endian order.

    uint32_t num = 9;
    uint8_t b[4];
    b[0] = (uint8_t) (num >>  0u);
    b[1] = (uint8_t) (num >>  8u);
    b[2] = (uint8_t) (num >> 16u);
    b[3] = (uint8_t) (num >> 24u);
    

    [2016 Edit] Simplification

    ... The type of the result is that of the promoted left operand.... Bitwise shift operators C11 §6.5.7 3

    Using a u after the shift constants (right operands) results in the same as without it.

    b3 = (num & 0xff000000) >> 24u;
    b[3] = (uint8_t) (num >> 24u);
    // same as 
    b3 = (num & 0xff000000) >> 24;
    b[3] = (uint8_t) (num >> 24);
    

提交回复
热议问题