Array of bytes to decimal value

Hi!

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

char hexInp = buffer[0] + buffer[1] + buffer[2] +buffer[3]
long decimal_answer = strtol(hexInp, NULL, 16)

Are you saying the value you are trying get is 0x000136c1?

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];

Thanks, fantastic, works perfect!!

ToddL1962:

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];

simnk:
Thanks, fantastic, works perfect!!

Do you understand why?

BTW, 7938810 = 1361C 16.

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.

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
  
  [color=#00979c]byte[/color] [color=#d35400]buffer[/color][color=#000000][[/color][color=#000000]4[/color][color=#000000]][/color][color=#000000];[/color]
  [color=#d35400]buffer[/color][color=#000000][[/color][color=#000000]0[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]0x00[/color][color=#000000];[/color]
  [color=#d35400]buffer[/color][color=#000000][[/color][color=#000000]1[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]0x01[/color][color=#000000];[/color]
  [color=#d35400]buffer[/color][color=#000000][[/color][color=#000000]2[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]0x36[/color][color=#000000];[/color]
  [color=#d35400]buffer[/color][color=#000000][[/color][color=#000000]3[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]0xC1[/color][color=#000000];[/color]
  
  [color=#00979c]uint32_t[/color] [color=#000000]as_int[/color][color=#000000];[/color]
  [color=#000000]static_assert[/color][color=#000000]([/color][color=#00979c]sizeof[/color][color=#000000]([/color][color=#d35400]buffer[/color][color=#000000])[/color] [color=#434f54]==[/color] [color=#00979c]sizeof[/color][color=#000000]([/color][color=#000000]as_int[/color][color=#000000])[/color][color=#434f54],[/color] [color=#005c5f]"Size mismatch"[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#d35400]memcpy[/color][color=#000000]([/color][color=#434f54]&[/color][color=#000000]as_int[/color][color=#434f54],[/color] [color=#d35400]buffer[/color][color=#434f54],[/color] [color=#00979c]sizeof[/color][color=#000000]([/color][color=#d35400]buffer[/color][color=#000000])[/color][color=#000000])[/color][color=#000000];[/color]

[color=#5e6d03]# if[/color] [color=#000000]BYTE_ORDER[/color] [color=#434f54]==[/color] [color=#000000]LITTLE_ENDIAN[/color]
  [color=#000000]as_int[/color] [color=#434f54]=[/color] [color=#000000]__builtin_bswap32[/color][color=#000000]([/color][color=#000000]as_int[/color][color=#000000])[/color][color=#000000];[/color]
[color=#5e6d03]# endif[/color]
  
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]as_int[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]

Pieter

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.

ToddL1962:
All valid points Pieter. I was just trying to provide a generic way without having to worry about endianness.

Sure, my reply wasn't directed to you, I already started typing before I saw your post :slight_smile:

Your approach will work perfectly fine in most cases.