How do I convert between big-endian and little-endian values in C++?

前端 未结 30 2513
难免孤独
难免孤独 2020-11-21 23:18

How do I convert between big-endian and little-endian values in C++?

EDIT: For clarity, I have to translate binary data (double-precision floating point values and 3

30条回答
  •  隐瞒了意图╮
    2020-11-21 23:54

    Using the codes below, you can swap between BigEndian and LittleEndian easily

    #define uint32_t unsigned 
    #define uint16_t unsigned short
    
    #define swap16(x) ((((uint16_t)(x) & 0x00ff)<<8)| \
    (((uint16_t)(x) & 0xff00)>>8))
    
    #define swap32(x) ((((uint32_t)(x) & 0x000000ff)<<24)| \
    (((uint32_t)(x) & 0x0000ff00)<<8)| \
    (((uint32_t)(x) & 0x00ff0000)>>8)| \
    (((uint32_t)(x) & 0xff000000)>>24))
    

提交回复
热议问题