Binary Data Services
Binary data is data whose unit can take on only two possible states, traditionally labeled as 0 and 1 in accordance with the binary numeral system and Boolean algebra. "bit" (binary digit) in computer science, truth value in mathematical logic and related domains, "binary variable" in statistics.
To read binary data files, define the variables, open the file for reading, and read the bytes into those variables. Each variable reads as many bytes out of the file as required by the specified data type and organizational structure.
How to Create Binary Files
- Add the namespace to the code page of your project. Writing and reading files requires the "IO" namespace.
- Create the filestream variable and assign it to a binary stream.
- Write to the binary file using the "Write" function.
- Close the file once all the information has been saved to the file.
This module performs conversions between Python values and C structs represented as Python bytes objects. This can be used in handling binary data stored in files or from network connections, among other sources. It uses Format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Python values.
By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details.
Functions and Exceptions
exception struct.error
Exception raised on various occasions; argument is a string describing what is wrong.
struct.pack(format, v1, v2, )
Return a bytes object containing the values v1, v2,packed according to the format string format. The arguments must match the values required by the format exactly.
struct.pack_into(format, buffer, offset, v1, v2,)
Pack the values v1, v2,according to the format string format and write the packed bytes into the writable buffer buffer starting at position offset. Note that offset is a required argument.
struct.unpack(format, buffer)
Unpack from the buffer buffer (presumably packed by pack(format, ...)) according to the format string format. The result is a tuple even if it contains exactly one item. The buffers size in bytes must match the size required by the format, as reflected by calcsize().
struct.unpack_from(format, /, buffer, offset=0)
Unpack from buffer starting at position offset, according to the format string format. The result is a tuple even if it contains exactly one item. The buffers size in bytes, starting at position offset, must be at least the size required by the format, as reflected by calcsize().
struct.iter_unpack(format, buffer)
Iteratively unpack from the buffer buffer according to the format string format. This function returns an iterator which will read equally-sized chunks from the buffer until all its contents have been consumed. The buffer%u2019s size in bytes must be a multiple of the size required by the format, as reflected by calcsize().
struct.calcsize(format)
Return the size of the struct (and hence of the bytes object produced by pack(format, ...)) corresponding to the format string format.details.
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
b'%uFFFD%uFFFD%uFFFD%uFFFD%uFFFD'
>>> unpack('hhl', b'%uFFFD%uFFFD%uFFFD%uFFFD%uFFFD')
(1, 2, 3)
>>> calcsize('hhl')
8
Classes
The struct module also defines the following type:
class struct.Struct(format)
Return a new Struct object which writes and reads binary data according to the format string format. Creating a Struct object once and calling its methods is more efficient than calling the struct functions with the same format since the format string only needs to be compiled once.
Note The compiled versions of the most recent format strings passed to Struct and the module-level functions are cached, so programs that use only a few format strings neednt worry about reusing a single Struct instance.
Compiled Struct objects support the following methods and attributes:
pack(v1, v2, ...)
Identical to the pack() function, using the compiled format. (len(result) will equal size.)
pack_into(buffer, offset, v1, v2, ...)
Identical to the pack_into() function, using the compiled format.
unpack(buffer)
Identical to the unpack() function, using the compiled format. The buffers size in bytes must equal size.
unpack_from(buffer, offset=0)
Identical to the unpack_from() function, using the compiled format. The buffers size in bytes, starting at position offset, must be at least size.
iter_unpack(buffer)
Identical to the iter_unpack() function, using the compiled format. The buffers size in bytes must be a multiple of size.
size
The calculated size of the struct (and hence of the bytes object produced by the pack() method) corresponding to format.
>>> pack('ci', b'*', 0x12131415)
b'*%uFFFD%uFFFD%uFFFD'
>>> pack('ic', 0x12131415, b'*')
b'*'
>>> calcsize('ci')
8
>>> calcsize('ic')
5
Comments