CANmessage help

would anyone know how to combine parts of a canmessage to an “int”
example
message.data = 01020304050607;
int myInt = message.data[3], message.data[4], message.data[5], message.data[6] ;

I was thinking something like the above. any help is appreciated.

message.data represents one bit where that bit is the x bit value of an 8, 16, 32 bit value.

The first 8-bits of any value would be like this:

bit = value
0 = 0x01
1 = 0x02
2 = 0x04
3 = 0x08
4 = 0x10
5 = 0x20
6 = 0x40
7 = 0x80

(0x being hex, base 16, prefix).

The long way would be to use bit shifting to make the value for each and then sum it.

message.data[3] << 3 … if the bit is set this value will be 0x08
message.data[4] << 4 … if the bit is set this value will be 0x10

int myInt = (message.data[3] << 3) + (message.data[4] << 4) + (message.data[5] << 5) + (message.data[6] << 6);

OR you can use bitmasking which would be the shorter way…

Let’s say you just want to know if bits 3, 4, 5, 6 are set in an 8 bit value and you specifically do NOT want to know the value of bits 0, 1, 2, and 7.

myInt = message.data & 0x78;

What the above will do is only set bits 3, 4, 5, 6 in your myInt value if they are set in the message.data value and it will specifically ignore the value of bits 0, 1, 2 and 7 if they are set.

That said, if bits 3-6 represent a distinct value, you need to shift that value over once you have it–

myInt = (message.data & 0x78) >> 3;