2
votes

I am sending data (signed 16bit fixed-point with 14 fractional bits) from one system to another. Due to constraints, the data has to be reinterpreted as uint16 before it is transmitted (i.e. bit representation is the same). The data ends up in Python but I am struggling to find a method to reinterpret this back to its original form.

For example: The original value is -0.123, reinterpreted as uint16 with value 63521.

How do I convert this back to the value -0.123 using Python?

Some More Examples

1.0450  -> 17121
0.9870  -> 16171
-0.9870 -> 49365
1
Can you provide a few more examples? With only one example, 16-bit fixed-point is not really enough to go on because it leaves too many possibilities open. For example: Binary or BCD? 2's complement or sign-and-magnitude? Big-endian or little-endian? A couple of additional examples would let us work out the answers.BoarGules
Yep, no problem. The original data is signed, big-endian. Not 2's compliment. I'm actually unsure as to whether it is BCD or binary encoded but I suspect it is the latter. I have edited my question with more examples. Thanks!jng
Are you sure it's not 2's complement? -0.9870 -> 49365 is consistent with 49365 -> -16171 / 2^14 = 0.986999harold
Yes you are right, I'm not sure how I missed that.jng

1 Answers

1
votes

A possible way to convert it back is:

def Q2_14toFloat(x):
    # convert unsigned to signed
    x = (x ^ 0x8000) - 0x8000
    # scale
    return x * (1.0 / 16384.0)