1

I have a IEEE-754 number being sent to a Raspberry-Pi running Python3. Its sent as four serial bytes, but I can't figure out how to assemble this into a floating point number in Python.

Tried the various posts about using struct.pack but can't get this to give a sensible result?

Fairly new to Python, so it might be a easy answer?

1
  • This looks like a good question, although it might be helpful to include some code snippets as to what you've tried.
    – Hannele
    Commented Jun 11, 2013 at 21:18

1 Answer 1

3

You want struct.unpack, not struct.pack:

struct.unpack('<f', bytes) # little-endian
struct.unpack('>f', bytes) # big-endian
1
  • 1
    +1. But you probably want to make the endianness explicit. Of course you have to know what the endianness is. If the docs for the sender don't say, I'd try !f as a first guess, and if that gives me garbage, use <f.
    – abarnert
    Commented Jun 11, 2013 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.