how to read and parse hex value data from text file
-
I have a text file where i store my hex data.
I store every package I receive from the serial, line by line.The records in the text file are as follows;
ffc48e3a0105038484848484848484
ffb88f3a0105038403000000000000my code is as follows;
QString filename= QFileDialog::getOpenFileName(this, "Choose File"); QFile file(filename); if(filename.isEmpty()) { return; } else if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) { return; } else { QByteArray ba = file.readAll(); }
When I read the file, it saves it in qbytearray like this;
but I want it to save 2 letters instead of one letter in each index.for example;
ba[0] = ff;
ba[1] = 07;can you help me how to do it?
-
@J-Hilk this is not the problem.
"ff" is my packet head.
Since "ff" is in separate indexes as 0 and 1, I cannot parse it.
if i do pckt=ba[0] i just get the letter f.
If I do memcpy(&pckt,&ba.data[0],2) I get a different value than 255. because ff isn't actually 1 byte not 2 byte. -
@KARMA said in how to read and parse hex value data from text file:
@J-Hilk this is not the problem.
"ff" is my packet head.
Since "ff" is in separate indexes as 0 and 1, I cannot parse it.
if i do pckt=ba[0] i just get the letter f.
If I do memcpy(&pckt,&ba.data[0],2) I get a different value than 255. because ff isn't actually 1 byte not 2 byte.no,
F
is a nibble, aka 4 bit
FF
is a byte aka 8 bit
QByteArray stores internally achar
array. char = 8 bit = 1 byte = int8_t -
@KARMA said in how to read and parse hex value data from text file:
I store every package I receive from the serial, line by line.
Since you say, and show, "line by line", you will have to deal with (skip) the newlines/line breaks anyway. With
readAll()
you will have to do that, apart from having to go through a pair of bytes at a a time.You might prefer to wrap the
QFile
into aQTextStream
so that you can useQString QTextStream::readLine()
repeatedly to get each line with the newline removed. That's aQString
, so you can go through it 2 characters at a time for your pairs of hex characters.Given a pair of characters, e.g.
"ff"
or"07",
you can then useint QString::toInt(nullptr, 16) const
to get the numeric value 0--255/0x00--0xFF of the character pair, if you need that, which it sounds like you do.Of course, if you don't want to do it via all these Qt methods you can always convert a pair of hexadecimal digit characters/bytes to the hex number yourself, with a single line of subtraction, multiplication and addition....
-
QByteArray data("ffc48e3a0105038484848484848484"); // the data are in hexa string representation QByteArray bin=QByteArray::fromHex(data); // convert to binary QBuffer buffer(&bin); buffer.open(QBuffer::ReadWrite); QDataStream stream(&buffer); stream.skipRawData(1); // skip first byte ff uint32_t v1; stream>>v1; // read uint32 // assuming the next data is an int16 as an example int16_t v2; stream>>v2; // read int16 etc ... qDebug()<<v1<<v2; // 3297655297 1283
You may have to consider endianess, here big endian (default)
see setByteOrder(QDataStream::ByteOrder bo)