How do you convert a base64 value to a hex value using Qt /KDE?
-
18-06-2021 - |
سؤال
I am working a software that uses Qt and has existing methods to encrypt/decrypt checksum values in hex. Currently, I've encountered some checksum values that are in base 64. Instead of reinventing the wheel to do the same stuff for base64 checksum, I was wondering if there is a way to convert a base64 value to a hex value using Qt or KDE libs . This way I'd save myself from writing semi duplicate code to do the almost similar task.
Here is how it should work:
Input: VO2S9pgCq1lqgTFTKssVj6amn0npNdagtjI8ziDtiRQ=
Output: 54ed92f69802ab596a8131532acb158fa6a69f49e935d6a0b6323cce20ed8914
The input is contained in a QString. Also in case there is no built in support in Qt/KDE can you suggest me a method in C++ ?
المحلول
This is what I tried right now:
QString b64("VO2S9pgCq1lqgTFTKssVj6amn0npNdagtjI8ziDtiRQ=");
QByteArray ba = QByteArray::fromBase64(b64.toAscii());
QString res(ba.toHex());
qDebug() << res;
Probably there's a more correct way to do it, but I'm too sleepy right now, sorry.
As a function:
/// untested
QString base64ToHex(const QString& b64){
QByteArray ba = QByteArray::fromBase64(b64.toAscii());
QString res(ba.toHex());
return res;
}
or simply..
QString base64ToHex(const QString& b64){
return QString(QByteArray::fromBase64(b64.toAscii()).toHex());
}
Edit 1: with Qt5 you should use QString::toLatin1()
instead of QString::toAscii()
.