A BinaryReader class for Python 3.6+.
Instanciate a BinaryReader class:
from binreader import BinaryReader
with open("example.bin", "rb") as f:
reader = BinaryReader(f)
print(reader.read_int()) # Reads an int32, returns a Python int
The following read methods are available:
read_bool()
->bool
read_byte()
->int
read_ubyte()
->int
read_int16()
->int
read_uint16()
->int
read_int32()
->int
read_uint32()
->int
read_int64()
->int
read_uint64()
->int
read_int()
->int
(alias ofread_int32()
)read_uint()
->int
(alias ofread_uint32()
)read_float()
->float
read_double()
->float
The following string read methods are available:
read_cstring()
->bytes
: Reads a null-terminated string.read_string(size: int=None)
->str
: Reads and decodes a string of sizesize
. Ifsize
isNone
, expects a null terminator.
The following utility methods are available:
align()
: Seeks forward to align the buffer's handle position on a multiple of 4 bytes.read(*args)
: Callsread(*args)
on the buffer.seek(*args)
: Callsseek(*args)
on the buffer.tell()
: Returns the buffer's handle position.read_struct(format)
: Read a struct from the buffer, returning a tuple of that struct. Size is automatically calculated. Endianness defaults to the reader's endianness, but may be overridden.
This project is licensed under the MIT license. The full license text is available in the LICENSE file.