2

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
jng
  • 23
  • 3
  • 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 Jul 21 '18 at 12:56
  • 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 Jul 21 '18 at 14:26
  • Are you sure it's not 2's complement? `-0.9870 -> 49365` is consistent with `49365 -> -16171 / 2^14 = 0.986999` – harold Jul 21 '18 at 14:30
  • Yes you are right, I'm not sure how I missed that. – jng Jul 21 '18 at 14:53

1 Answers1

1

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)
harold
  • 61,398
  • 6
  • 86
  • 164
  • This is a great and simple solution, solving a massive headache for me - thank you! – jng Jul 21 '18 at 15:12