Converting canbus data to an integer

i have the following value that i want to extract an integer value from but i can't find out how

CanMsg const msg = CAN.read();
Serial.println(msg);

Output:
[010] (2) : 01D0

Welcome to the forum

Which integer do you want to extract and will it always be in the same place in the message and the same length ?

i want to convert the hexadecimal 01d0 to an int and it will always be in the same place

what you have NOT made clear though is whether your 'integer' value is 8 or 16 bits long.

if 8 bit the link I share has the solution.

if 16 bit the you need to know which of the 2 bytes is the MSbyte.

so for example assuming '01' is MSByte then using this snippet will generate the 16 bit integer.

uint16_t val = msg.data[0];

val = (val<<8)|msg.data[1];

Serial.print(val, DEC);

hope that helps

that works now, thank you very much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.