简体   繁体   中英

Convert hexadecimal string to QByteArray

I need to convert a QString which is already in hexadecimal format to a QByteArray . For example:

QString a = "AF5603B4"

Should be stored in QByteArray as:

QByteArray ba[4] = { 0xAF, 0x56, 0x03, 0xB4 }

How do I do this in Qt 5.9? I have tried using many methods but all of these convert the string characters to their ASCII values and then give that hexadecimal value.

I found Convert.toByte method to use in C# ; is there an equivalent in Qt I can use?

You can use ByteArray::fromHex function like this:

QString MyHexString ="AF5603B4";
QByteArray cmd = QByteArray::fromHex(MyHexString.toUtf8());

Output :

截图

And to convert QByteArray to Hex string:

QByteArray cmd;
QString NewHexString = cmd.toHex();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM