How can I convert unsigned int to unsigned char

  • A+

I'm trying to interface a digital sensor module to an Arm Cortex M4F chip.

The data received is 3 bytes from the module. However the only function I have available in my MCU is a uint32_t read function. I don't know why there isn't a uint8_t function but maybe it's just a matter of shifting bits? I'm learning c++ as I go here.

So i read the value as:

uint32_t value = mcu_read(pin); 

value now contains an uint32_t, 4 bytes. I need the first three. Is it possible to shift it in any way to "extract" them out? Does this make sense? The showstopper is the mcu_read function that reads 4 bytes minimum. If there where a uint8_t read function I could just call it three times to get the data I needed.


Something like this?

uint32_t value = mcu_read(pin); uint8_t* bytes = (uint8_t*)(&value); byte_0 = bytes[0]; byte_1 = bytes[1]; byte_2 = bytes[2]; byte_3 = bytes[3]; 

Be careful with endianess


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: