Basic integer hex conversion

thoraxe

Member
I am using the FlexCAN library. The message array is a bunch of integers.

In my particular case, I have two integers that actually represent a two-byte larger integer.

The hex string '10E' (integer 270) has come through as an array with 16 (10 hex) and 14 (E hex). So, the array looks like:

bytearray[0] = 14 // E
bytearray[1] = 16 // 10

I'm failing to understand how to take these two ints and convert them to the actual int. Converting the ints back to hex and then back to a big int seems odd, and I can't really figure out how to do that, either.

Sorry, this low-level byte magic C++ is beyond me.
 
In the end when in a variable of the proper size - it is binary in memory. But it has been parsed as noted into two unique byte values for a larger Hex number.

How it is displayed base 2, 10, or 16 is just output manipulation of those binary bits.

So : uint32_t bigInt = bytearray[1] * 256 + bytearray[0];

Will convert those two bytes of Hex into a single integer in memory.

<edited> : 256 mult not 16
 
Last edited:
Your code does not seem to work for me.

Code:
MB 0  OVERRUN: 0  LEN: 8 EXT: 0 TS: 39395 ID: 589 Buffer: 0 0 0 0 0 1 80 0
Lo word: 1   Hi word: 128
Can Speed: 384
2049

The hex string is 0x180 split into 0x01 and 0x80 (384 = 0x180)
0x01 is 1
0x80 is 128

Your code produces a value of 2049 (128*16 + 1).
If I reverse it, it would only be 16+128 = 144.

I had found this thread:
https://forum.pjrc.com/threads/3259...gasquirt-CAN-Bus?p=95027&viewfull=1#post95027

Code:
  can_speed = (int16_t((hi_word) | (lo_word << 8)));

The above produces 384, which was the input.
 
You haven't separated the hex correctly. The hex 10E has an implied leading zero and should be 010E which in your bytearray would be
bytearray[0] = 14 // E
bytearray[1] = 01 // 1

and now you can use
bytearray[1]*256 + bytearray[0]
which will produce 270.

Pete
 
Hmmm, or perhaps what you've called a hex string, 10E, is actually three ASCII characters and would be stored as:
bytearray[0] = 'E';
bytearray[1] = '0';
bytearray[2] = '1';

Pete
 
Edited p#2 - if as described with byte value 0-255 it should have been mult of *256 not *16

But that didn't match the math as presented 1*256+14 versus 0x10*16 + 14 to get 270 ????
 
Last edited:
Or just do simple casting, no unnecessary calculations needed

Code:
uint16_t bigInt = 0;
byte *b = (byte *)&bigInt;
b[0] = bytearray[0];
b[1] = bytearray[1];
or (depending on the byte order)
Code:
uint16_t bigInt = 0;
byte *b = (byte *)&bigInt;
b[0] = bytearray[1];
b[1] = bytearray[0];
 
In the end when in a variable of the proper size - it is binary in memory. But it has been parsed as noted into two unique byte values for a larger Hex number.

How it is displayed base 2, 10, or 16 is just output manipulation of those binary bits.

So : uint32_t bigInt = bytearray[1] * 256 + bytearray[0];

Will convert those two bytes of Hex into a single integer in memory.

<edited> : 256 mult not 16

With your edit, I think this should work.
 
Back
Top