I have a BLE buffer which contain 4 Hex digits like:

buff=00:05:C4:7B

I can manually convert 0005C47B to decimal =377979

How can I do this in code going from hex buffer to decimal

I have a BLE buffer which contain 4 Hex digits like:

buff=00:05:C4:7B

I can manually convert 0005C47B to decimal =377979

How can I do this in code going from hex buffer to decimal

@Athnetix, one way uses the `sscanf()`

C++ function:

You can use `sscanf()`

to parse out each HEX value as an unsigned integer then sum them in their corrected bit order. For example, if `00:05:C4:7B`

is parsed to variables a,b,c,d then the order-corrected sum of the values is:

`unsigned value = a<<24 + b<<16 + c<<8 + d;`

where â€ś<<Xâ€ť is â€śleft shift by X bitsâ€ť which is the same as multiplying by 2 to the X power.

5 Likes

Thanks peekay that works great. The part that was getting me was how best to put the bytes back together.

Just for completeness, if you had a valid HEX string already you could also use `strtol(s, NULL, 16)`

to convert that into a numeric value.

3 Likes