How to Split Hex String?
-
QDataStream is the way to go:
char hex[]="d5 0a 1b 73 0f fe 0f ff 00 af 0a 15 aa"; QByteArray data=QByteArray::fromHex(hex); QDataStream stream(data); stream.setByteOrder(QDataStream::BigEndian); uint8_t bn, nb, esc, s, cs; uint16_t d1,d2,d3,d4; stream>>bn>>nb>>esc>>s; stream>>d1>>d2>>d3>>d4; stream>>cs; qDebug()<<bn<<nb<<esc<<s<<cs; qDebug()<<d1<<d2<<d3<<d4;
logs:
213 10 27 115 170
4094 4095 175 2581 -
QDataStream is the way to go:
char hex[]="d5 0a 1b 73 0f fe 0f ff 00 af 0a 15 aa"; QByteArray data=QByteArray::fromHex(hex); QDataStream stream(data); stream.setByteOrder(QDataStream::BigEndian); uint8_t bn, nb, esc, s, cs; uint16_t d1,d2,d3,d4; stream>>bn>>nb>>esc>>s; stream>>d1>>d2>>d3>>d4; stream>>cs; qDebug()<<bn<<nb<<esc<<s<<cs; qDebug()<<d1<<d2<<d3<<d4;
logs:
213 10 27 115 170
4094 4095 175 2581 -
@ahsan737 I don't believe you are understanding what you are asking/doing!
First, please learn what is a "Base Numbered System":
- base 2 (aka Binary)
- base 8 (aka Octal)
- base 10 (aka Decimal)
- base 16 (aka Hexadecimal)
There are many, many explanation available on internet, for example ==> https://www.mathsisfun.com/binary-decimal-hexadecimal.html
Then you will learn that:
- 80 in hexadecimal equals 128 in decimal
- 0A in hexadecimal equals 10 in decimal
- etc.
-
@KroMignon You completely misunderstood the question. Of course, I totally understand this base numbered system. I am not talking about Hex to Dec conversion. My question is about string format as it is supposed to start with "80" but it is ending up at that, so I want to ask how can I sort it out? (given that I do not have access to change incoming data format)
-
@ahsan737 I don't understand what you are trying to do.
QByteArray
is a container for char (qint8
).
If you what to convert the content of a QByteArray variable into hex string, simply read the documentation and you will found QbyteArray::toHex().QByteArray macAddress = QByteArray::fromHex("123456abcdef"); macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef" macAddress.toHex(0); // returns "123456abcdef"
If you want to convert a hex string to
QByteArray
, use QbyteArray::fromHex().QByteArray text = QByteArray::fromHex("517420697320677265617421"); text.data(); // returns "Qt is great!"
To decode binary data from
QByteArray
, the easiest way it do useQDataStream
(see post from @mpergand)So what is your problem?
-
@ahsan737
Then @KroMignon is not the only one who does not understand what you are saying/asking for.I have looked at your "attached picture" and I do not understand what it is you want to achieve. You show an "Actual Outcome" which has two windows, one showing some hex numbers and the other the decimal equivalents. You show a "Desired Outcome" which has two lines, one showing hex numbers and the other the decimal equivalents. I don't understand what you want to do to what to achieve what.
If all you want to do is take either a decimal or a hex string of numbers and output a string in the other base, what is there to say other than you need to convert the input string in the correct base to its number and then convert that to string in the desired output base?
I do not know what your "I am not able to make changes to the incoming data format. " signifies, there is no need to change incoming format.
-
@KroMignon
@JonB
thanks to both of you for paying attention, Now I try to explain at my best.
Please look at the incoming data pattern in the Desired Outcome, it is starting with "80 0A" but if you look at the Actual Outcome section (green Hex numbers), "80 0A" is the end of the line and then next line starts. So my concern is how can I deal with this situation in order to split data correctly in relevant parts. I hope now I have explained it well.