Construct a QByteArray from a HEX value entered as a QString - qt

If QString str = "0xFFFF", how to turn this text representation of an hex into a QByteArray? In the end, I would like to have the same of what I get from the following:
QByteArray ba;
ba.resize(2);
ba[0] = 0xFF;
ba[1] = 0xFF;

In either case the final QByteArray would be a sequence of hex values, i.e. FFFF. Conversion should be applied on that string. Given that, if your input string is provided prepended with the 0x, you should get rid of it via mid().
Here is a code snippet which compares the results of the two approaches: manually filling the QByteArray with hex values or converting hex values from a QString:
QByteArray array1;
array1.resize(2);
array1[0] = 0xFF;
array1[1] = 0xFF;
QString str = "0xFFFF";
QString value = str.mid(2); // "FFFF" <- just the hex values!
QByteArray array2 = QByteArray::fromHex(value.toLatin1());
qDebug() << array1; // not printable chars
qDebug() << array2;
qDebug() << array1.toHex(); // to have a printable form use "toHex()"!
qDebug() << array2.toHex();

Related

How can i convert a QByteArray into a hex string?

I have the blow QByteArray.
QByteArray ba;
ba[0] = 0x01;
ba[1] = 0x10;
ba[2] = 0x00;
ba[3] = 0x07;
I have really no idea how to convert this QByteArray into resulted string which have "01100007", which i would use the QRegExp for pattern matching on this string?
First of all, the QByteArray does not contain "hex values", it contains bytes (as it's name implies). Number can be "hex" only when it is printed as text.
Your code should be:
QByteArray ba(4, 0); // array length 4, filled with 0
ba[0] = 0x01;
ba[1] = 0x10;
ba[2] = 0x00;
ba[3] = 0x07;
Anyway, to convert a QByteArray to a hex string, you got lucky: just use QByteArray::toHex() method!
QByteArray ba_as_hex_string = ba.toHex();
Note that it returns 8-bit text, but you can just assign it to a QString without worrying much about encodings, since it is pure ASCII. If you want upper case A-F in your hexadecimal numbers instead of the default a-f, you can use QByteArray::toUpper() to convert the case.
QString has following contructor:
constructor QString(const QByteArray &ba)
But note that an octal number is preceeded by 0 in c++, so some of your values are deciamal, some octal, none of them are hex.

Get a string representation of a single byte from QByteArray?

I have a QByteArray i create manually:
QByteArray hexArray(QByteArray::fromHex("495676"));
If this was encoded ASCII it would be "IVv".
If I want to get a single byte of data from that array.
I can do that like this:
qDebug() << messageToBeSent_raw[0];
However, that outputs I, which is correct but I would like to get 49. What I'm looking for is an equivalent of the QByteArray::toHex() just for a single byte. Is there a way to do it?
You can use QString::number.
qDebug() << QString::number(hexArray[0], 16);

Compare raw hexadecimal values in Qt?

Below a QbyteArray is created and it contains a byte with the hexadecimal value 49. 49 is one byte of data written in memory, it isn't an ASCII string representation of 49 (which is 2 bytes)
QByteArray data(QByteArray::fromHex("49"));
At some point I populate the data with additional bytes.
data.append(QByteArray::fromHex("7656"))
How to compare a single byte with a hex value?
Example:
This passes (49 hex is 73 decimal):
if (data.at(0) == 73)
qDebug() << "True"
But i need to work with hex values:
if (data.at(0) == WHAT_HERE?("49"))
qDebug() << "True"
In C/C++, a hexadecimal integer literal is prefixed with 0x. So:
if (data.at(0) == 0x49)
qDebug() << "true";
if you know it at compile time then you can do 0x49
if you get a QString to compare it with then
QString test="47";
bool ok;
int result = test.toInt(&ok,16);
if(ok && data.at(0)==result){
//they are equal
}

Serial port communication in Qt

I am new to Qt and need to prepare a project to send hex commands from rs232.
QString line contains 64bit binary data which i have to convert into hexadecimal and send it through rs232 .
QString a=ui->comboBox->currentText();
QString s1;
s1="./calc "+a;
QProcess p1;
p1.start(s1);
p1.waitForFinished(-1);
QString line ;
//read
QFile file("TeleOutput.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in (&file);
line = in.readAll();
ui->plainTextEdit->setPlainText(line);
So, how to convert 64 bit binary data in QString line to hexadecimal value and transfer it through rs232?
First of all - you should really use QtSerialPort
Second of all - QString is a class, which works with actual string. QByteArray works with raw data. When you write QString line = in.readAll(); it implicitly calls QString(const QByteArray &ba), which uses QString::fromAscii.
Last of all, if you want to process 64bit integers, you should do something like this:
quint64 d;
QDataStream stream(&file);
while (!stream.atEnd())
{
stream >> d;
process(d);
}
Update
Quote:
My problem is that in plainTextEdit
"1111110101000101010101010101010101010101010101010101010......." 64
bit data is populated , i need to convert this data into hex and send it through rs232
Solution:
QString binData = plainTextEdit.toPlainText();
QByteArray result;
while (binData.size() >= 64)
{
quint64 d;
QString dataPiece = binData.left(64);
binData.remove(0, 64);
d = dataPiece.toULongLong(0, 2);
result += QByteArray::number(d);
}
_com->write(result);
_com->flush();
Where _com is a pointer to QtSerialPort, with all parameters set and opened without errors.

How can I convert integer to qbitarray?

How can I get QBitArray from qint value? I need to change some bits in number, so I want to use QBitArray for it.
QBitArray's '>>' operator expects bitset size as the first 4 bytes, so JustMaximumPower's snipper won't work. Correct data stream should look like this:
QBitArray bits;
quint32 size = 32;
quint32 value = 1337;
QByteArray data;
QDataStream stream(data, QIODevice::ReadWrite);
stream << size << value;
stream.device()->seek(0);
stream >> bits;
Actually I don't find QBitArray very useful. You could try std::vector<bool> for variable size or std::bitset for fixed size (both from STL library).
std::bitset<32> bits(1337);
bits[0] = 1;
Not testet but should work:
qint value = 1337;
QBitArray bits;
QDataStream stream;
stream << value;
bits << stream;
If you want to just change some bits, QBitArray is overkill.
int bitmask = 1 << 3; // let's change 4th bit
bitmask = 0x55555555; // or perhaps all odd bits
int number = 12345678;
number |= bitmask; // set to 1s
number &= ~bitmask; // set to 0s
number ^= bitmask; // negate what's already there

Resources