hbutils.encoding.decode

Overview:

Functions to deal with encoding binary data easily.

auto_decode

hbutils.encoding.decode.auto_decode(data: bytes, encoding: Optional[str] = None, prefers: Optional[List[str]] = None) → str[source]
Overview:

Auto decode binary data to string, the encoding mode will be automatically detected.

Arguments:
  • data (bytes): Original binary data to be decoded.

  • encoding (Optional[str]): Encoding mode to be used, default is None which

    means this function need to automatically detect the encoding.

  • prefers (Optional[List[str]]): Prefered encodings.

Returns:
  • str (str): Decoded string.

Examples:

>>> auto_decode(b'kdsfjldsjflkdsmgds')  # 'kdsfjldsjflkdsmgds'
>>> auto_decode(b'\xd0\x94\xd0\xbe\xd0\xb1\xd1\x80\xd1\x8b\xd0\xb9 \xd0'
...             b'\xb2\xd0\xb5\xd1\x87\xd0\xb5\xd1\x80')  # "Добрый вечер"
>>> auto_decode(b'\xa4\xb3\xa4\xf3\xa4\xd0\xa4\xf3\xa4\xcf')  # "こんばんは"
>>> auto_decode(b'\xcd\xed\xc9\xcf\xba\xc3')  # "晚上好"