Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 16-Jun-16 6:23am
Comments
Richard MacCutchan 16-Jun-16 10:41am    
There is no simple conversion since the original characters are in string form. You will have to extract each pair from the string and convert it to its hex value.
Member 12587659 16-Jun-16 10:52am    
extracting pair by shifting and masking?
Richard MacCutchan 16-Jun-16 10:54am    
OK, and what is the problem?
Member 12587659 16-Jun-16 10:59am    
I was asking for the method of extracting pairs from Qstring.
Richard MacCutchan 16-Jun-16 11:13am    
Pity you did not mention that in your question. I am not a QT user, but looking at the documentation I see QString QString::right(int n) const which, with the other referenced methods, should do what you want.

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

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:
C++
str.remove(QChar(' '));
 
Share this answer
 
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)

C++
// 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);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900