# CAN bus data conversion issue

**URL:** <https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466>\
**Category:** Programming\
**Created:** [January 21, 2026, 9:24am UTC](https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466 "2026-01-21T09:24:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ardubit](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardubit/32/264049_2.png) [@Ardubit](https://forum.arduino.cc/u/Ardubit)\
**Post date:** [January 21, 2026, 9:24am UTC](https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466/1 "2026-01-21T09:24:02Z")

</div>

I am getting an encoder Angle information via a CAN message. The first two bytes hold the information. I am using the following code to extract same :

```cpp
unsigned int axle1_RawAngle = (rxBuf[1] << 8) | rxBuf[0];
      float aX1G = (axle1_RawAngle / 256) - 125; 
      Serial.println(axle1_RawAngle, HEX);
      Serial.println(aX1G);

```

With the above conversion I expect to read as below :

0x5F00 should read -30 Deg

0X7D00 should read 0 Deg

0x9B00 should read +30 Deg

Values above 0X7D00 work as expected . But values below 0x7D00 result in 65535. Possibly there is a error in the bit shift operation and storing in INT … the rXBuf[8] is declared as char.

Where is this going wrong ?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [January 21, 2026, 9:47am UTC](https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466/2 "2026-01-21T09:47:29Z")

</div>

> [@Ardubit](#):
>
> `float aX1G = (axle1_RawAngle / 256) - 125; `

===\>

```arduino
    float aX1G = (axle1_RawAngle / 256.0) - 125.0;

```

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [January 21, 2026, 9:50am UTC](https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466/3 "2026-01-21T09:50:44Z")

</div>

what microcontroller are you using?

with problems like this it is a good idea to implement a simple program to test the algorithm, e.g.

```cpp
void decode(uint8_t* rxBuf) {
  int axle1_RawAngle = (rxBuf[1] << 8) | rxBuf[0]; // << change to signed int
  float aX1G = (axle1_RawAngle / 256) - 125;
  Serial.println(axle1_RawAngle, HEX);
  Serial.println(aX1G);
}

void setup() {
  Serial.begin(115200);
  delay(2000);
  uint8_t d[] = { 0x00, 0x7d };
  decode(d);
  uint8_t d1[] = { 0x00, 0x5f };
  decode(d1);
  uint8_t d2[] = { 0x00, 0x9b };
  decode(d2);
}

void loop() {}

```

when run on an ESP32 displays

```arduino
7D00
0.00
5F00
-30.00
9B00
30.00

```

---

<div class="post-metadata">

**Author:** ![Ardubit](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardubit/32/264049_2.png) [@Ardubit](https://forum.arduino.cc/u/Ardubit)\
**Post date:** [January 21, 2026, 10:31am UTC](https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466/5 "2026-01-21T10:31:55Z")

</div>

Perfect …. the data type definition was the issue. The moment I changed from Char to uint8\_t all things fell in place !! I am using a Mega 2560 mcu

```cpp
unsigned int axle1_RawAngle = (rxBuf[1] << 8) | rxBuf[0]; // Axle 1 Angle
aX1G = (float(axle1_RawAngle) / 256) - 125; 

```

Thanks for the quick help !!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2026, 10:32am UTC](https://forum.arduino.cc/t/can-bus-data-conversion-issue/1426466/6 "2026-07-20T10:32:51Z")

</div>

Closed on July 20, 2026, 10:32am UTC

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