无符号数 高低字节变换位置

 

//两个字节换位
quint16 exchangeByte(unsigned short val)
{
    unsigned char h = val >> 8;
    unsigned char l = val & 0xFF;
    unsigned short ret = l;
    ret <<= 8;
    ret |= h;

    return ret;
}
//val 变为从高到低,如0x11223344变为0x44332211
quint32 exchangeByte(quint32 val)
{
    quint32 ret;
    unsigned char b1, b2, b3, b4;//字节由高到低
    b4 = val & 0xFF;
    b3 = (val >> 8) & 0xFF;
    b2 = (val >> 16) & 0xFF;
    b1 = (val >> 24) & 0xFF;
    ret = b4;
    ret <<= 8;
    ret |= b3;
    ret <<= 8;
    ret |= b2;
    ret <<= 8;
    ret |= b1;
    return ret;
}

 

posted @ 2024-02-05 13:50  阳光下的小土豆  阅读(24)  评论(0编辑  收藏  举报