How to Split Hex String?
-
wrote on 18 May 2020, 08:53 last edited by
-
wrote on 18 May 2020, 16:25 last edited by
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 -
Greetings,
I am receiving a string in Hex format in the following pattern:
How can I split this string into relevant pieces of data?
Looking forward to your kind responses.
@ahsan737 how do you receive the data, why is it in a String in the first place and not a QByteArray?
-
Greetings,
I am receiving a string in Hex format in the following pattern:
How can I split this string into relevant pieces of data?
Looking forward to your kind responses.
-
wrote on 18 May 2020, 08:59 last edited by
I am receiving this data over the Bluetooth connection from another device. Before I had tried data transfer using "Bluetooth Terminal" apps available for android. But now I want to implement in Qt app for Android.
-
@ahsan737
So every 2 characters is a hex number for a byte. Read and convert those, or useQByteArray QByteArray::fromHex(const QByteArray &hexEncoded)
to do the lot in one go. -
@JonB thank you, that's what I am confused about that how can I convert and split the data correctly.
-
wrote on 18 May 2020, 15:22 last edited by
Maybe show us some code of what you tried?
-
wrote on 18 May 2020, 16:25 last edited by
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 2581wrote on 19 May 2020, 07:25 last edited by ahsan737@mpergand thank you so much, you've made my day.
Now I am facing this issue (explained in the attached picture).
I am not able to make changes to the incoming data format. So how can I parse the incoming data correctly?
-
@JonB sorry for the vague question but #mpergand has already responded to my query in great detail. Thank you for your kind response.
wrote on 19 May 2020, 07:40 last edited by@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.
-
@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.
wrote on 19 May 2020, 07:59 last edited by ahsan737@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)
-
@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)
wrote on 19 May 2020, 08:33 last edited by KroMignon@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?
-
@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)
wrote on 19 May 2020, 08:35 last edited by@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.
-
@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.
wrote on 19 May 2020, 08:46 last edited by@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. -
@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.@ahsan737 You simply start to output the values at the wrong index (see the line "00 00 80 0A"). You need to locate 80 first and start from there.
-
@ahsan737 You simply start to output the values at the wrong index (see the line "00 00 80 0A"). You need to locate 80 first and start from there.
-
@jsulm the confusion is that it won't be 80 every time, this number will keep updating with every chunk of data.
@ahsan737 Do you have any kind of protocol?! How do you know where a block starts and ends?
9/19