Python Struct
Python
Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way. The python language can also be used in web development; Django and Flask are the frameworks used to create web applications using Python. In Python, indentation is the main concept; if we do not follow proper indentation, the code may not run. We can easily create an application in Python if we are familiar with indentation, Variables, Operators, loop concepts and function concepts in python language. The python programming language has pre-installed modules, which help perform various operations, and one of the modules is the struct module.
Struct Module in Python
The struct module is available in the python programming language; this module helps convert the Python values and the C structs; these Cstructs are represented as the python bytes objects.
This module helps handle the binary data stored in the files. The Format strings are the mechanism used to specify the expected layout when packing and unpacking the data. The struct module is available in Python 3. x, so for running the code, we need to use the Python3 interpreter, and it is not available in Python 2. x so that we can run the code in other interpreters rather than Python 3.
Struct Functions
There are many built-in functions available in the struct module. The struct function takes the buffer argument, the objects in the module use the buffer protocol, and in return, it will provide us with the readable or read-writeable buffer. The most commonly used types for this buffer protocol are the bytes and bytearray. Other types are viewed as the array of bytes that will implement the buffer protocol so that this data can be read without additional copying from the bytes object.
Now let us look at some of the built-in functions available in the struct module:
1. struct.pack( ):
This method or function will return the string containing the values a1,a2,a3,a4…. these values are packed according to the given format; the Format strings is a mechanism used to specify the expected layout when the packing and the unpacking of the data are done. We need to follow the format used by the Format strings; otherwise, it will raise the struct.error
Syntax:
Struct.pack( format, a1, a2, a3 ,……)
Parameters:
- Format: This is the format used to format the given data into the required format.
- a1, a2, a3…: These are the values provided to the function that must be formatted.
Code:
# importing the library
importstruct
# Format: d is short in C type
# Format: a is int in C type
#Format: ‘daa', which stands for 'short intint.'
data = struct.pack(‘daa’, 1, 2, 3)
print( data )
# Format: j is long in C type
# Format: ‘jjj’ stands for ‘ longlonglong ‘
data =struct.pack(‘jjj’, 1,2,3)
print(data)
Output:
b’\x01\x00\x02\x00\x00\x01\x02\x02\x00\x01\x00\x00\x01\x02\x00\x01\x01\x00’
b’\x01\x00\x01\x00\x02\x00\x01\x02\x01\x02\x01\x03\x01\x01\x01\x00\x01\x01’
2. Struct.unpack( ):
This method or function will return the unpacked values a1, a2, a3, a4…. these values are packed according to the format; the Format strings are a mechanism used to specify the expected layout when the packing and the unpacking of the data are done. These values are returned as a tuple whose size equals the total number of values passed to the struct.pack( ) during the packing.
Syntax:
Struct.unpack(format, string)
Parameters:
- Format: This is the format used to format the given data into the required format.
- String: This string will contain unpacked values using the unpack( ) function.
Code:
# importing the library
importstruct
# Format: d is short in C type
# Format: a is int in C type
#Format: ‘daa', which stands for 'short intint.'
data = struct.pack(‘daa’, True, 1, 2, 3)
print( data )
# Using the struct.unpack( ) function
# The variables a1,a2,a3,…. these values are returned as the elements in the tuple
struct.unpack(‘data, data)
tup)
Output:
b’\x01\x00\x02\x00\x00\x01\x02\x02\x00\x01\x00\x00\x01\x02\x00\x01\x01\x00’
(True, 1, 2, 3)
b’\x01\x00\x02\x00\x00\x01\x02\x02\x00\x01’
(1, 3.6567)
Here the ‘b’ stands for the binary output. We can observe that the output is returned the values in the form of a tuple as we used the unpack( ) function.