-1

I am looking for basically the exact same thing as this question but in Python.

How can I obtain the bytes in memory of a single-precision floating point variable in Python? For instance if I have a variable x = 0.65 I need to get some form of the list [97, 102, 38, 63].

PGmath
  • 381
  • 3
  • 16

2 Answers2

0

Bytes:

x = 0.65
print(list(struct.pack("!f", x)))
0

Try:

import struct

ba = bytearray(struct.pack("f", 0.65))
print(ba[0], ba[1], ba[2], ba[3])
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432