Convert 0xAABBCCDD to QString DDCCBBAA



  • Hello,
    I need to convert for example from int 0xAABBCCDD to QString DDCCBBAA. I think it's any function of QByteArray but I can't find more.

    I have this code, but It doesn't work properly, output is last two letters.

    #define NUMBER           0xAAFC
    
        QByteArray array;
        array.append(NUMBER);
    
        qDebug() << QString(array.toHex());
    

    Thanks for help, byte actions aren't my friends.



  • The fact that you used symmetric bytes (e.g. AA instead of A1) makes it somewhat ambiguous. See if this suits you

    QString hexString = QString::number(0xA1B2C3D4,16);
    std::reverse(hexString.begin(),hexString.end());
    

    this will produce 4D3C2B1A

    if you wanted D4C3B2A1 instead you can indeed use bitshift:

    QString hexString;
    for(auto number = 0xA1B2C3D4;number;number >>=8)
    hexString.append(QString::number(number & 0xFF,16));
    

    EDIT

    Fixed error in code



  • @pVit

    QString hexString = QString::number(0xAABBCCDD,16);
    std::reverse(hexString.begin(),hexString.end());

    works fine to have upper case
    add
    QString s = hexString.toUpper();
    you will get string in upper case



  • @VRonin
    Thanks a lot. I only fixed number >>=1 to number >>=8 for correct function. Now it works as I need.



  • I only fixed number >>=1 to number >>=8

    Yep, sorry you are correct, I was thinking in bytes for some reason. Need more ☕


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.