Write a Python program to Convert Binary tuple to Integer
The program is written in Python and it converts a tuple of binary values to its decimal representation. Here's an explanation of how it works:
- The tuple val is defined as (1, 0, 1, 0), which represents a binary number.
- The original tuple is printed using the print() function: print("Original tuple : " ,val). The output will be Original tuple : (1, 0, 1, 0).
- The binary tuple is then converted to a string representation using "".join(str(i) for i in val). This converts each element of the tuple to a string using str(i) and then concatenates all the strings into a single string. For the given tuple, the resulting string is "1010".
- The string is then converted to an integer using int("".join(str(i) for i in val), 2). The second argument 2 specifies that the string is a binary number. The int() function converts the binary string to a decimal integer.
- The resulting decimal integer is stored in the variable res. The decimal number is then printed using the print() function: print("Decimal number is : " ,res). The output will be Decimal number is : 10.
In summary, the program takes a tuple of binary values and converts it to a decimal representation using string manipulation and the int() function.
Source Code
val = (1 , 0 , 1 , 0)
print("Original tuple : " ,val)
res = int("".join(str(i) for i in val), 2)
print("Decimal number is : " ,res)
Output
Original tuple : (1, 0, 1, 0)
Decimal number is : 10