| Start |
|---|
Format a list of values into a byte object |
Unpack a byte object according to a format string |
| Packing a structure The module "struct" provides facility to pack python objects as contiguous chunk of bytes or dissemble a chunk of bytes to python structures. The pack function takes a format string and one or more arguments, and returns a binary string. This looks very much like you are formatting a string except that the output is not a string but a chunk of bytes. Output: Native byteorder: little Byte chunk: '\x03\x00\x00\x00\x04\x00\x05' Byte chunk unpacked: (3, 4, 5) Byte chunk: '\x03\x00\x00\x00\x04\x00\x05\x00' You could use network byte order with data received from network or pack data to send it to network. Output: Byte chunk native byte order: '\x03\x00\x04\x00\x05\x00' Byte chunk network byte order: '\x00\x03\x00\x04\x00\x05' You can optimize by avoiding the overhead of allocating a new buffer by providing a buffer that was created earlier. Output: Byte chunk: '\x03\x00\x04\x00\x05\x00\x00\x00' Byte chunk: '\x00\x00\x03\x00\x04\x00\x05\x00' Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference