Qstring到qbytearray [英] Qstring to qbytearray

查看:208
本文介绍了Qstring到qbytearray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有QString格式的8字节数据我正在从txt文件中读取它。

QString stringData =02 FA 7B 3A 64 9D FF CA;



现在我想把它转换成QByteArray

所需的输出类似于

例如

QByteArray DataBytes
DataBytes [0] = 0xCA

DataBytes [1] = 0xFF

DataBytes [2] = 0x9D

DataBytes [3 ] = 0x64等等



任何建议和解决方案都将受到赞赏。



我是什么尝试过:



我以前用字符串拆分做这个,但后来我意识到这是错误的方法。

I have 8 bytes data in QString format I am reading it from txt file.
QString stringData= "02 FA 7B 3A 64 9D FF CA";

now i want to convert it into QByteArray
required output is something like
e.g
QByteArray DataBytes
DataBytes[0] = 0xCA
DataBytes[1] = 0xFF
DataBytes[2] = 0x9D
DataBytes[3] = 0x64 so on

any suggestion and solutions would be appreciated.

What I have tried:

I was previously doing this with string spliting but lateron i realize this is wrong method.

推荐答案

QByteArray :: fromHex() [ ^ ]功能:

There is the QByteArray::fromHex()[^] function:
QByteArray ar = QByteArray::fromHex(str.toUTF8());



该函数需要一个字节数组作为输入。这是由 toUTF8()提供的,当输入字符串仅包含ASCII字符时,它返回一个有效数组。



到目前为止我还没有用过,但文档说明跳过了无效字符,这些字符应该适用于你的例子中的空格。或者你可以在删除之前:


That function requires a byte array as input. That is provided by toUTF8() which returns a valid array when the input string contains only ASCII characters.

I have not used that so far but the documentation states that invalid characters are skipped which should apply to the spaces in your example. Alternatively you might remove them before:

str.remove(QChar(' '));


Hello



因为每个元素stringData是一个char,解决问题的一种方法可能如下。(我假设每对char子串之间有一个空格。如果没有空格,你应该能够一次选择两个字符来自tempArray并将它们转换为字节(整数)



Hello

Because every element of stringData is a char, one way of solving the problem might be the following.(I presume that there is a space between each pair of char substring. In case there is no space, you should be able to pick two chars at a time from the tempArray and convert them to byte(integer)

// Tranfer the elements of stringData to a temporary QbyteArray
QByteArray tempArray = stringData.toLatin1();
// now the character representation is in tempArray
// We need to pick two char elements of tempArray at a time.
QList<QByteArray>qbList = tempArray.split(' ');
// each element of qbList is a QByteArray of 2 elements
// There is no effort to check for compliance of the elements of qbList.
// There is no effort to check for any semantic errors within the context of supplied data.
// We reserve space for dataBytes
QByteArray dataBytes;
dataBytes.reserve(qbList.count());
// we make the convertion, realizing that the first element correspond to the
// last element of dataBtes, and so on.
int dbIndex = qbList.count() - 1;
bool ok;
for (int i = 0; i < qbList.count(); i++, dbIndex --) {
  unsigned char c;
  c = qbList.at(i).toUInt(&ok, 16) & 0xff; // guard against sign extension
  dataBytes[dbIndex] = c;
}
// check the values in dataBytes
for (int i = 0; i < dataBytes.count(); i++) {
  printf("0x%02x\n", dataBytes[i] & 0xff);
}


这篇关于Qstring到qbytearray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆