Convert 0xAABBCCDD to QString DDCCBBAA
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
if you wanted
D4C3B2A1instead you can indeed use bitshift:
QString hexString; for(auto number = 0xA1B2C3D4;number;number >>=8) hexString.append(QString::number(number & 0xFF,16));
Fixed error in code
QString hexString = QString::number(0xAABBCCDD,16);
works fine to have upper case
QString s = hexString.toUpper();
you will get string in upper case
Thanks a lot. I only fixed number >>=1 to number >>=8 for correct function. Now it works as I need.
I only fixed
Yep, sorry you are correct, I was thinking in bytes for some reason. Need more ☕