Python – Interconvert Tuple to Byte Integer

Sometimes, while working with Python data, we can have a problem in which we need to perform conversion of tuple values, into combined byte and then to integer and vice-versa. This kind of problem can have application in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = (1, 2, 3, 4, 5)
Output : 4328719365

Input : test_int = 4328719365
Output : (1, 2, 3, 4, 5)

Method #1 : Tuple -> Byte Integer : Using int.from_bytes()
The combination of above functions can be used to solve this problem. In this, we perform the task of conversion using internal function from_bytes() and obtain the desired integer value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Interconvert Tuple to Byte Integer
# Tuple -> Byte Integer : Using int.from_bytes()
  
# initializing tuples
test_tuple = (6, 8, 3, 2)
  
# printing original tuple
print("The original tuple : " + str(test_tuple))
  
# Interconvert Tuple to Byte Integer
# Tuple -> Byte Integer : Using int.from_bytes()
res = int.from_bytes(test_tuple, byteorder ='big')
  
# printing result 
print("Tuple after conversion : " + str(res))

chevron_right


Output :



The original tuple : (6, 8, 3, 2)
Tuple after conversion : 101188354

 

Method #2 : Byte Integer -> Tuple : Using tuple.to_bytes()
The combination of above functions can be used to solve this problem. In this, we perform the task of conversion using internal method to_bytes() to obtain desired result.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Interconvert Tuple to Byte Integer
# Using Byte Integer -> Tuple : Using tuple.to_bytes()
  
# initializing integer
test_int = 101188354
  
# printing original integer
print("The original integer : " + str(test_int))
  
# Interconvert Tuple to Byte Integer
# Using Byte Integer -> Tuple : Using tuple.to_bytes()
res = tuple(test_int.to_bytes(4, byteorder ='big'))
  
# printing result 
print("Integer after conversion : " + str(res))

chevron_right


Output :

The original integer : 101188354
Integer after conversion : (6, 8, 3, 2)

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.