简体   繁体   English

将十六进制字符串转换为QByteArray

[英]Convert hexadecimal string to QByteArray

I need to convert a QString which is already in hexadecimal format to a QByteArray . 我需要将已经是十六进制格式的QString转换为QByteArray For example: 例如:

QString a = "AF5603B4"

Should be stored in QByteArray as: 应存储在QByteArray中:

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

How do I do this in Qt 5.9? 我如何在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. 我尝试过使用很多方法,但所有这些方法都将字符串字符转换为ASCII值,然后给出十六进制值。

I found Convert.toByte method to use in C# ; 我发现在C#中使用Convert.toByte方法; is there an equivalent in Qt I can use? 我可以使用Qt中的等价物吗?

You can use ByteArray::fromHex function like this: 您可以像这样使用ByteArray::fromHex函数:

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

Output : 输出

截图

And to convert QByteArray to Hex string: 并将QByteArray转换为十六进制字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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