Really dont know how to solve this. I have an array of 4 bytes fetched from a serial reading. These 4 bytes need to be "grouped" togeteher and converted from HEX into DECIMAL value.
byte buffer[5];
Serial1.readBytes(buffer, 5);
// Fetched values in HEX are now: buffer[0] = 00, buffer[1] = 01, buffer[2] = 36, buffer[3] = C1
How can I convert these into decimal? Expected value is 79388
Have tried the following using strtol without any luck
unsigned long value = 0;
value += (unsigned long)buffer[0] << 24;
value += (unsigned long)buffer[1] << 16;
value += (unsigned long)buffer[2] << 8;
value += (unsigned long)buffer[3];
The data you have is not in "HEX" format, it's in binary format in a register in the microcontroller. It's not a string either, so strtol is not relevant here.
There are two steps involved: 1) convert the byte buffer to a 4-byte integer, 2) convert that integer from network byte order to the host byte order.
The first step uses memcpy, the second uses ntohl. Sadly, the latter is not available on Arduino, but you can use the GCC builtin bswap32 routine.
PieterP:
The data you have is not in "HEX" format, it's in binary format in a register in the microcontroller. It's not a string either, so strtol is not relevant here.
There are two steps involved: 1) convert the byte buffer to a 4-byte integer, 2) convert that integer from network byte order to the host byte order.
The first step uses memcpy, the second uses ntohl. Sadly, the latter is not available on Arduino, but you can use the GCC builtin bswap32 routine.
All valid points Pieter. I was just trying to provide a generic way without having to worry about endianness.