Tag Archives: uint16

Split and merge a 32-bit float with two 16-bit unsigned integers

I am sharing a code with an example that splits and merges a 32-bit float with two 16-bit unsigned integers.

Especially, when you work with an embedded system, 16-bit unsigned integer (or 8-bit unsigned integer, that is char) is a common format for data transmission. You can use this function when you want to send a 32-bit float variable with these 16-bit data format.

You can also use this function without the LSB-part. For the details, see the code below.

Sending 32-bit float variable with 16-bit data format

I needed to send a 32-bit float variable with a 16-bit data format. In this case, we can transmit the data by sacrificing some less significant bits. This is the code for the conversion of 32-bit float into 16-bit unsigned integer, and re-conversion from the 16-bit unsigned integer to 32-bit float. This code would be especially useful when you program some embedded system.

This is the result of execution of this code.

I got some ideas from this thread.
https://stackoverflow.com/questions/21005845/how-to-get-float-bytes

Good luck!