Skip to content

Instantly share code, notes, and snippets.

@kai3341
Last active February 25, 2022 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kai3341/27e158bb0a9163f1603902abaeb9e940 to your computer and use it in GitHub Desktop.
Save kai3341/27e158bb0a9163f1603902abaeb9e940 to your computer and use it in GitHub Desktop.
python: reading float80 values
#!/usr/bin/env python3
import zlib
import base64
import ctypes
import struct
def decode_str64(str64):
taillen = ctypes.sizeof(ctypes.c_longdouble) - 10
if (taillen < 0):
raise Exception
else:
tail = taillen * b'\00'
data = zlib.decompress(base64.b64decode(str64)) # <== This is array of float80
return ((ctypes.c_longdouble.from_buffer(bytearray(i + tail))).value for i in struct.unpack(int(len(data)/10) * '10s', data))
STR64 = '''eNozBoPPHewODDQDD6tE1rk//NXK5qAZ03/oq4bMX3YHD7Cg4m92h1kzQUDyH7tD0A651teBFb/YkdQd/qoBZJcC1Z09AwZAWaBhQM3H/7A7FNpyXV9cUPuD3QGoEah7dTMhf4ANjuGfz4ZkL8R9VT/ZHSCy538imwyigZxvyOrAyvqbgGJvQNa+vvgDWRZi8s2vyGLfNMBm4zBPHuL8ryxkxALU5OusUFOO/kX2BwBGxJWV'''
print(tuple(decode48(STR64)))
# (273.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 133.98, 506.22, 502.26, 508.2, 500.94, 506.22, 502.92, 501.6, 505.56, 496.98, 263.34, 0.0, 0.0, 0.0, 159.06, 508.2, 498.96, 499.62, 496.98, 493.68, 498.96, 493.02, 497.64, 498.96, 491.7, 498.96, 496.32, 493.68, 498.96, 61.38, 0.0, 0.0, 0.0, 0.0, 0.0, 107.58, 507.54, 499.62)
@Konstantin-Dudersky
Copy link

Thank you, very help

@kai3341
Copy link
Author

kai3341 commented Feb 25, 2022

@Konstantin-Dudersky use numpy instead. Numpy works a little bit faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment