Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating a float to unsigned into and back

Tags:

c

I am currently working in C with a microcontroller and in order to transmit a reading over Bluetooth I have to send it as a uint8. The reading is currently in the form of a float. I've been trying to think of how to do this and then be able to convert it back to a float on the reciever end but have no ideas. I'm limited because I can't utilize a custom structure or anything because it has to be in a uint8 to transmit.

Any suggestions?

I have tried a couple things but none have gotten very far so I don't have much to show for it

like image 268
Boltaction Avatar asked Nov 28 '25 19:11

Boltaction


1 Answers

Any type in C can be traversed using character pointers and on all sensible compilers, uint8_t is a character type. For example:

const uint8_t* ptr = (const uint8_t*)&some_var;
for(size_t i=0; i<sizeof(some_var); i++)
{
  send_byte(ptr[i]);
} 

Keep in mind however that your CPU endianess might not match network endianess, in which case you need to swap the byte order before sending.

like image 65
Lundin Avatar answered Nov 30 '25 09:11

Lundin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!