Sascha M Holesch
Sascha M Holesch

Reputation: 53

How do convert number with binary exponent -10 into float?

I am working with a bluetooth low energy device on iOS with Swift receiving some data. The said data is specified in https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.csc_measurement.xml as a uint16. It is represented using a binary exponent, but I couldn't find how to convert that into a float for example to further work with it.

Last Wheel Event Time Information: Unit has a resolution of 1/1024s.

Unit: org.bluetooth.unit.time.second

Exponent: Binary, -10

I thought to just count 10 bits in and use the first segment as part before the decimal point and the second segment as the part after. But the result seems wrong.

Any hints or solutions are greatly appreciated.

Thank you very much.

Upvotes: 2

Views: 731

Answers (2)

Matt
Matt

Reputation: 459

To further expand on the answer above by hotpaw2-

General:

Exponent: Binary, 0 => 001 => 1 decimal Exponent: Binary, -1 => 0.1 => 1/(2^1) => 1/2 => 0.5 decimal Exponent: Binary, -2 => 0.01 => 1/(2^2) => 1/4 => 0.25 decimal Exponent: Binary, -3 => 0.001 => 1/(2^3) => 1/8 => 0.125 decimal

Now for your particular case defined in the Bluetooth SIG specification for the Cycling Power Measurement Characteristic

Last Wheel Event Time Information: Unit has a resolution of 1/1024s.

Unit: org.bluetooth.unit.time.second

Exponent: Binary, -10

. Exponent: Binary,-10 => 0.0000000001 => 1/(2^10) => 1/1024 => 0.0009765625 decimal

Now for the BLE characteristic value data packet that comes in from the peripheral/server with the resolution 1/1024s: . Last_revolution_event_time

Time(seconds) = Last_revolution_event_time * 1/1024

Upvotes: 4

hotpaw2
hotpaw2

Reputation: 70703

A binary exponent of -10 is the same as converting the uint16 value to Double and dividing by 1024.0 (or multiplying by 1.0 over 2 to the 10th).

Upvotes: 4

Related Questions