hbutils.binary.float

CFloatType

class hbutils.binary.float.CFloatType(mark: str, size: int)[source]
Overview:

Float type, based on struct module.

__init__(mark: str, size: int)

Constructor of CMarkedType.

Parameters:
  • mark – Mark of the type.

  • size – Size of the type.

property mark

Mark of the type, will be used to read from binary data with struct module.

read(file: BinaryIO) → float[source]

Read from binary with struct module.

Parameters:

file – Binary file, io.BytesIO is supported as well.

Returns:

Result value.

property size

Size of the given type.

write(file: BinaryIO, val: Union[int, float])[source]

Write value to binary IO with struct module.

Parameters:
  • file – Binary file, io.BytesIO is supported as well.

  • val – Writing value.

c_float

hbutils.binary.float.c_float = <hbutils.binary.float.CFloatType object>

Alias for c_float32.

c_double

hbutils.binary.float.c_double = <hbutils.binary.float.CFloatType object>

Alias for c_float64.

c_float16

hbutils.binary.float.c_float16 = <hbutils.binary.float.CFloatType object>
Overview:

Reading and writing half-precision(16-bits) float.

c_float32

hbutils.binary.float.c_float32 = <hbutils.binary.float.CFloatType object>
Overview:

Reading and writing single-precision(32-bits) float.

Examples::
>>> import io
>>> import math
>>> from hbutils.binary import c_float32
>>>
>>> with io.BytesIO(b'\x00\x00\x90\x7f'
...                 b'\x00\x00\x80\x7f'
...                 b'\x00\xa0\x3e\xc1'
...                 b'\x00\x00\x70\x00') as file:
...     print(c_float32.read(file))
...     print(c_float32.read(file))
...     print(c_float32.read(file))
...     print(c_float32.read(file))
nan
inf
-11.9140625
1.0285575569695016e-38
>>> with io.BytesIO() as file:
...     c_float32.write(file, math.nan)
...     c_float32.write(file, +math.inf)
...     c_float32.write(file, -11.9140625)
...     c_float32.write(file, 1.0285575569695016e-38)
...     print(file.getvalue())
b'\x00\x00\xc0\x7f\x00\x00\x80\x7f\x00\xa0>\xc1\x00\x00p\x00'

c_float64

hbutils.binary.float.c_float64 = <hbutils.binary.float.CFloatType object>
Overview:

Reading and writing double-precision(64-bits) float.

Examples::
>>> import io
>>> import math
>>> from hbutils.binary import c_float64
>>>
>>> with io.BytesIO(b'\x00\x00\x00\x00\x00\x00\xf8\x7f'
...                 b'\x00\x00\x00\x00\x00\x00\xf0\x7f'
...                 b'\x00\x00\x00\x00\x00\xd4\x27\xc0'
...                 b'\x00\x00\x00\x00\x00\x00\x0c8') as file:
...     print(c_float64.read(file))
...     print(c_float64.read(file))
...     print(c_float64.read(file))
...     print(c_float64.read(file))
nan
inf
-11.9140625
1.0285575569695016e-38
>>> with io.BytesIO() as file:
...     c_float64.write(file, math.nan)
...     c_float64.write(file, +math.inf)
...     c_float64.write(file, -11.9140625)
...     c_float64.write(file, 1.0285575569695016e-38)
...     print(file.getvalue())
b"\x00\x00\x00\x00\x00\x00\xf8\x7f\x00\x00\x00\x00\x00\x00\xf0\x7f\x00\x00\x00\x00\x00\xd4'\xc0\x00\x00\x00\x00\x00\x00\x0c8"