How to convert hex array data to integer ?
-
I have sensor data in below format:
int arr [10] ={0xAA, OxBB};
How to convert this 2 different index data into integer [dec] value ?
here 0th and 1st index hex data represent one sensor data.
-
@J-Hilk here first byte is high and 2nd byte is low. then how to combine it ?
int arr [10] ={0xAA, OxBB}; int16_t value = (0xAA << 8) | 0xBB; // (arr[0] << 8) | arr[1]
-
I have sensor data in below format:
int arr [10] ={0xAA, OxBB};
How to convert this 2 different index data into integer [dec] value ?
here 0th and 1st index hex data represent one sensor data.
@Qt-embedded-developer
As said before, data bytes are not decimal or hexadecimal, they are just bits/numbers. I believe you have asked this type of question before. There is no "conversion" to do. You have an array with room for 10int
s, it has the first 2 values in it, they areint
s. -
@Qt-embedded-developer
As said before, data bytes are not decimal or hexadecimal, they are just bits/numbers. I believe you have asked this type of question before. There is no "conversion" to do. You have an array with room for 10int
s, it has the first 2 values in it, they areint
s.actually my aim to how to combine this 2 data into one integer value ?
-
actually my aim to how to combine this 2 data into one integer value ?
@Qt-embedded-developer that depends, what's the high byte, what low ?
-
@Qt-embedded-developer that depends, what's the high byte, what low ?
@J-Hilk here first byte is high and 2nd byte is low. then how to combine it ?
-
@J-Hilk here first byte is high and 2nd byte is low. then how to combine it ?
int arr [10] ={0xAA, OxBB}; int16_t value = (0xAA << 8) | 0xBB; // (arr[0] << 8) | arr[1]
-
actually my aim to how to combine this 2 data into one integer value ?
@Qt-embedded-developer said in How to convert hex array data to integer ?:
actually my aim to how to combine this 2 data into one integer value ?
That was not clear (at least, not to me)! :)
-
@Qt-embedded-developer said in How to convert hex array data to integer ?:
actually my aim to how to combine this 2 data into one integer value ?
That was not clear (at least, not to me)! :)
@JonB actually i face one interview question where they asked to me that
if you have array which store hex data. where first 2 index show one sensor data. that sensor data you need to store it in one integer value. How to do this ? I hope this is clear. if not then let me know why ?
-
@JonB actually i face one interview question where they asked to me that
if you have array which store hex data. where first 2 index show one sensor data. that sensor data you need to store it in one integer value. How to do this ? I hope this is clear. if not then let me know why ?
@Qt-embedded-developer said in How to convert hex array data to integer ?:
if you have array which store hex data
There is no such thing as "hex [integer/numeric] data". A number is a number is a number. Unless you/they meant "a string of hex digits as characters", which you would need to convert to a number first, but I doubt they said that, or at least not as you phrased it.
-
@Qt-embedded-developer said in How to convert hex array data to integer ?:
if you have array which store hex data
There is no such thing as "hex [integer/numeric] data". A number is a number is a number. Unless you/they meant "a string of hex digits as characters", which you would need to convert to a number first, but I doubt they said that, or at least not as you phrased it.
-
@JonB I would have given/phrased the task this way:
Given an array
arr
, where each entry has the size of 2 nibble.
Formulate the most generic way to combine consecutive entries to an integer. -
here's an alternate solution.
std::vector<uint8_t> data = { 0x12U, 0x34U, 0x00U, 0x10U }; uint32_t v = 0U; memcpy(&v, data.data(), sizeof(v)); // to change endianness if necessary uint8_t vPtr = &v; for (size_t i = 0U; i < sizeof(v) / 2U; i++)) { std::swap(*(vPtr + i), *(vPtr + 3U - i)); } // v now contains a 32 bit uint comprised of the data from the vector
-
Endianess? The little endian that could, or the big endian that interneted?
I like unions for this kind of stuff.
@fcarney can you give example for same using union
-
@fcarney can you give example for same using union
@Qt-embedded-developer keep in mind, that unions are more generally speaking a C remnant.
For most cases C++ offers better and saver alternatives.
If you use a union for low level stuff like here in this example, you're most likely be fine, but if you start stuffing full fetched classes /structs into unions, you'll most likely run into lifetime/constructor/destructor issues and other UB's -
@Qt-embedded-developer said in How to convert hex array data to integer ?:
can you give example for same using union
union { uint16_t u16; struct { uint8_t low; uint8_t high; } bytes; uint8_t bytearray[2]; } un1; un1.u16 = 0x4321; qDebug() << Qt::hex << un1.bytes.low; qDebug() << Qt::hex << un1.bytes.high; qDebug() << Qt::hex << un1.bytearray[0]; qDebug() << Qt::hex << un1.bytearray[1];
If you are doing a large set it is probably just as easy to do shifting.
You have to be very careful with data sizes here. That is why I used explicitly sized data types. -
you guys are missing an esoteric problem that is common in embedded systems. data alignmnent is not guaranteed. That's why brute force (while more complex) is more reliable. If using unions then at least make sure to pragma pack() your structs. There is also this c++ language spec annoyance that says the compiler is free to rearrange struct members to optimize memory usage. While I've never seen is happen in the real world, making assumptions about data ordering "could" bite you in the butt.