Python - Interconvert Tuple to Byte Integer


The tuple is ordered or a finite sequence to collect the data. It can be represented by using parenthesis i.e. (). A byte integer is a simple number without having decimal or fractional values. In Python, we have some built-in functions such as from_bytes(), bytes(), unpack(), and, enumerate() will be used to Interconvert Tuple to Byte Integer.

Syntax

The following syntax is used in the examples

from_bytes()

The from_bytes() is an in-built function in Python that accepts two specific parameters

  • bytes()- The bytes() is also a built-in function that defines the immutable series of integers.

  • byteorder- The byteorder determines the integer representation using setting the value as big.

bytes()

The bytes() is an in-built function in Python that returns the byte object. The byte object immutable series of integers that ranges between 0 - 256.

unpack()

The unpack() is an in-built function of Python that allows us to provide all datatype of an iterable object such as a list, set, etc.

enumerate()

The enumerate() is an in-built function in Python that keep track of particular iteration of each given index element.

Using from_bytes()

In the following example, we will start the program by storing the tuple in the variable tup_val. Then use the built-in function from_bytes that accepts two parameters- bytes() and byteorder to solve the interconvert tuple into byte integer and store it in the variable byte_int. Finally, we are printing the result with the help of variable byte_int.

Example

tup_val = (45, 5, 55, 19, 20)
byte_int = int.from_bytes(bytes(tup_val), byteorder='big')
print("Conversion of tuple into byte integer:\n", byte_int)

Output

 Conversion of tuple into byte integer:
 193361023764

Using Struct Module

In the following example, the program uses a struct module that will be used to change the interconversion of tuple into integer byte form. Then create the tuple in the variable tuple_val. Next, use the built-in function unpack() with struct module that accepts parameters-

  • !i - The value should be represented as a single signed byte or least significant byte.

  • struct.pack('!BBBB', *tuple_val)- The struct is the name of the module and it is associated with the built-in function pack() which packed the 4 unsigned bytes. The value of unsigned byte ranges between 0-255.

Finally, we are printing the result with the help of variable byte_int.

Example

import struct
tuple_val = (10, 20, 30, 40)
byte_int = struct.unpack('!i', struct.pack('!BBBB', *tuple_val))[0]
print("Conversion of tuple into byte integer:\n", byte_int)

Output

 Conversion of tuple into byte integer:
 169090600

Using enumerate() Function

In the following example, begin the program by storing the tuple elements in the variable tuple_val. Next, using the built-in function sum() and enumerate() will calculate the interconversion of tuple byte and generate the result.

Example

tuple_val = (45, 5, 55, 19, 20)
byte_int = sum((x << (8 * i)) for i, x in enumerate(tuple_val[::-1]))
print("Conversion of tuple into byte integer:\n", byte_int)

Output

 Conversion of tuple into byte integer:
 193361023764

Conclusion

We discussed the three different methods to convert the tuple element into a byte integer. There were various built-in methods used such as enumerate(), unpack(), etc. that help to set the specific conditions and operations. There are some applications related to this program such as Operating System, digital code, etc.

Updated on: 16-Aug-2023

622 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements