hbutils.binary.str

CStringType

class hbutils.binary.str.CStringType(encoding=None)[source]
Overview:

Simple string type.

It should end with a single \x00, which is quite common in C language.

__init__(encoding=None)[source]

Constructor of CStringType.

Parameters:

encoding – Encoding type, default is None which means auto-detect the encodings.

property encoding

Encoding type.

read(file: BinaryIO) → str[source]

Read simple string value.

Parameters:

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

Returns:

String value.

write(file: BinaryIO, val: str)[source]

Write simple string value to binary IO object.

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

  • val – String to write.

c_str

hbutils.binary.str.c_str = <hbutils.binary.str.CStringType object>
Overview:

Reading and writing simple string, ends with a single \x00.

Examples::
>>> import io
>>> from hbutils.binary import c_str
>>> 
>>> with io.BytesIO(
...         b'kdsfjldsjflkdsmgds\x00'
...         b'\xd0\x94\xd0\xbe\xd0\xb1\xd1\x80\xd1\x8b\xd0\xb9 \xd0'
...         b'\xb2\xd0\xb5\xd1\x87\xd0\xb5\xd1\x80\x00'
...         b'\xa4\xb3\xa4\xf3\xa4\xd0\xa4\xf3\xa4\xcf\x00'
...         b'\xcd\xed\xc9\xcf\xba\xc3\x00'
... ) as file:
...     print(c_str.read(file))
...     print(c_str.read(file))
...     print(c_str.read(file))
...     print(c_str.read(file))
kdsfjldsjflkdsmgds
Добрый вечер
こんばんは
晚上好
>>> with io.BytesIO() as file:
...     c_str.write(file, "kdsfjld")
...     c_str.write(file, "Добрый")
...     print(file.getvalue())
b'kdsfjld\x00\xd0\x94\xd0\xbe\xd0\xb1\xd1\x80\xd1\x8b\xd0\xb9\x00'

CSizedStringType

class hbutils.binary.str.CSizedStringType(size: int, encoding=None)[source]
Overview:

Sized string type.

It should have a fixed size, which is defined like char s[size] in C language.

__init__(size: int, encoding=None)[source]

Constructor of CStringType.

Parameters:
  • size – Size of the string’s space.

  • encoding – Encoding type, default is None which means auto-detect the encodings.

property encoding

Encoding type.

read(file: BinaryIO) → str[source]

Read sized string value.

Parameters:

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

Returns:

String value.

property size

Size of the given type.

write(file: BinaryIO, val: str)[source]

Write sized string value to binary IO object.

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

  • val – String to write.

c_sized_str

hbutils.binary.str.c_sized_str(size: int)hbutils.binary.str.CSizedStringType[source]
Overview:

Reading and writing sized string, which occupy a fixed space..

Parameters:

size – Size of the string’s space.

Examples::
>>> import io
>>> from hbutils.binary import c_sized_str
>>> 
>>> with io.BytesIO(
...         b'kdsfjld\x00\x00\x00\x00\x00\x00\x00\x00'
...         b'\xd0\x94\xd0\xbe\xd0\xb1\xd1\x80\xd1\x8b\xd0\xb9\x00\x00\x00'
... ) as file:
...     print(c_sized_str(15).read(file))
...     print(c_sized_str(15).read(file))
kdsfjld
Добрый
>>> with io.BytesIO() as file:
...     c_sized_str(15).write(file, "kdsfjld")
...     c_sized_str(15).write(file, "Добрый")
...     print(file.getvalue())
b'kdsfjld\x00\x00\x00\x00\x00\x00\x00\x00\xd0\x94\xd0\xbe\xd0\xb1\xd1\x80\xd1\x8b\xd0\xb9\x00\x00\x00'