Convert bytestring to float in python
Question:
I am working on a project where I read data which is written into memory by a Delphi/Pascal program using memory mapping on a Windows PC. I am now mapping the memory again using pythons mmap and the handle given by the other program and as expected get back a bytestring.
I know that this should represent 13 8-byte floating-point numbers but I do not know how I could correctly convert this bytestring back into those. I also know the aproximate value of the floating-point numbers to check my results.
The code I am using to get the bytestring looks like this:
import mmap
import time
size_map = 13*8
mmf_name = "MMF"
mm = mmap.mmap(-1, size_map, mmf_name, access=mmap.ACCESS_READ)
while True:
mm.seek(0)
mmf = mm.read()
print(mmf)
time.sleep(0.04)
mm.close()
For now I am just running the code again every 40 ms because the data is written every 40 ms into memory.
The output looks something like this:
b'xcdxccxccxe0xe6vxb9xbfx9ax99x99!Fxcd&@xf5xa2xc5,.xafxbdxbfx95xb0xeaxb5xaenxd9?333/x9bx165@x00x00x00hx89D1xc08xd1xc3xc3x92x82xf7?tAx8fBxd6Gx04@]xc1xedx98rAx07@x9ax99x99x99x99x191@x00x00x00xc0xccxcc=@x00x00x00xc0x1eE7@x00x00x00x00xb8x1ex1a@'
I tried struct.unpack()
, .decode()
and float.fromhex()
to somehow get back the right value but it didn’t work. For example the first 8 bytes should roughly represent a value between -0.071 and -0.090.
The problem seems to be very basic but I still wasn’t able to figure it out by now. I would be very grateful for any suggestions how to deal with this and get the right floating-point values from a bytestring. If I am missing any needed Information I am of course willing do give that too.
Thank you!
Answers:
Try using numpy
s frompuffer
function. You will get an array you can than read:
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
import numpy as np
buffer = b'xcdxccxccxe0xe6vxb9xbfx9ax99x99!Fxcd&@xf5xa2xc5,.xafxbdxbfx95xb0xeaxb5xaenxd9?333/x9bx165@x00x00x00hx89D1xc08xd1xc3xc3x92x82xf7?tAx8fBxd6Gx04@]xc1xedx98rAx07@x9ax99x99x99x99x191@x00x00x00xc0xccxcc=@x00x00x00xc0x1eE7@x00x00x00x00xb8x1ex1a@'
nparray = np.frombuffer(buffer, dtype=np.float64)
nparray
array([ -0.09947055, 11.40092568, -0.11595429, 0.39127701,
21.08830543, -17.26772165, 1.46937825, 2.53507664,
2.90695686, 17.1 , 29.79999924, 23.27000046,
6.52999878])
nparray[0]
-0.09947055
I am working on a project where I read data which is written into memory by a Delphi/Pascal program using memory mapping on a Windows PC. I am now mapping the memory again using pythons mmap and the handle given by the other program and as expected get back a bytestring.
I know that this should represent 13 8-byte floating-point numbers but I do not know how I could correctly convert this bytestring back into those. I also know the aproximate value of the floating-point numbers to check my results.
The code I am using to get the bytestring looks like this:
import mmap
import time
size_map = 13*8
mmf_name = "MMF"
mm = mmap.mmap(-1, size_map, mmf_name, access=mmap.ACCESS_READ)
while True:
mm.seek(0)
mmf = mm.read()
print(mmf)
time.sleep(0.04)
mm.close()
For now I am just running the code again every 40 ms because the data is written every 40 ms into memory.
The output looks something like this:
b'xcdxccxccxe0xe6vxb9xbfx9ax99x99!Fxcd&@xf5xa2xc5,.xafxbdxbfx95xb0xeaxb5xaenxd9?333/x9bx165@x00x00x00hx89D1xc08xd1xc3xc3x92x82xf7?tAx8fBxd6Gx04@]xc1xedx98rAx07@x9ax99x99x99x99x191@x00x00x00xc0xccxcc=@x00x00x00xc0x1eE7@x00x00x00x00xb8x1ex1a@'
I tried struct.unpack()
, .decode()
and float.fromhex()
to somehow get back the right value but it didn’t work. For example the first 8 bytes should roughly represent a value between -0.071 and -0.090.
The problem seems to be very basic but I still wasn’t able to figure it out by now. I would be very grateful for any suggestions how to deal with this and get the right floating-point values from a bytestring. If I am missing any needed Information I am of course willing do give that too.
Thank you!
Try using numpy
s frompuffer
function. You will get an array you can than read:
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
import numpy as np
buffer = b'xcdxccxccxe0xe6vxb9xbfx9ax99x99!Fxcd&@xf5xa2xc5,.xafxbdxbfx95xb0xeaxb5xaenxd9?333/x9bx165@x00x00x00hx89D1xc08xd1xc3xc3x92x82xf7?tAx8fBxd6Gx04@]xc1xedx98rAx07@x9ax99x99x99x99x191@x00x00x00xc0xccxcc=@x00x00x00xc0x1eE7@x00x00x00x00xb8x1ex1a@'
nparray = np.frombuffer(buffer, dtype=np.float64)
nparray
array([ -0.09947055, 11.40092568, -0.11595429, 0.39127701,
21.08830543, -17.26772165, 1.46937825, 2.53507664,
2.90695686, 17.1 , 29.79999924, 23.27000046,
6.52999878])
nparray[0]
-0.09947055