项目作者: sergeyshilin

项目描述 :
Python binary object reader/writer
高级语言: Python
项目地址: git://github.com/sergeyshilin/byter.git
创建时间: 2019-09-21T18:06:29Z
项目社区:https://github.com/sergeyshilin/byter

开源协议:Apache License 2.0

下载


Byter

Python reader/writer for binary objects

Installation

  1. pip install -U byter

Usage Example

  1. with open("/path/to/binary/file", "rb") as data:
  2. has_data = read_bool(data)
  3. year = read_short(data)
  4. month = read_short(data)
  5. width = read_float(data)
  6. height = read_float(data)
  7. text = read_string(data, 70)
  8. array = read_array(data, 3, 'unsigned_short')
  9. print("has_data:", has_data)
  10. print("year:", year)
  11. print("month:", month)
  12. print("width:", width)
  13. print("height:", height)
  14. print("text:": text)
  15. print("array:", array)
  16. >> has_data: True
  17. year: 2019
  18. month: 9
  19. width: 1280.0
  20. height: 1024.0
  21. text: "Hello World!"
  22. array: [13, 4, 16]

Types allowed to read/write

C Type Python type Size in bytes
char string of length 1 1
signed char integer 1
unsigned char integer 1
bool boolean 1
short integer 2
unsigned short integer 2
int integer 4
unsigned int integer 4
long integer 4
unsigned long integer 4
long long integer 8
unsigned long long integer 8
float float 4
double float 8
char[] string

Methodes allowed

For each C type from the table in the previous section, there is a read_ and a write_ function that performs a reading/writing of a specified number of bytes from the binary object. For example,

  1. a = read_unsigned_long(data)

will read 4 consequent bytes from the binary source data.

However, to read an array of chars (string) the method is

  1. s = read_string(data, s_len)

It is also possible to read an array of bytes using the following method:

  1. arr = read_array(data, size, 'unsigned_short')

This will read a sequence of length size of unsigned ints, size * 2 bytes in total (short type is of 2 bytes).