[英]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.