- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Convert Gray Code to Binary
When it is required to convert gray code to binary code, a method is defined, that checks to see if the number is 0 or not.
Below is the demonstration of the same −
def flip_num(my_nu): return '1' if(my_nu == '0') else '0'; def gray_to_binary(gray): binary_code = "" binary_code += gray for i in range(1, len(gray)): if (gray[i] == '0'): binary_code += binary_code[i - 1] else: binary_code += flip_num(binary_code[i - 1]) return binary_code gray_code = "01101001" print("The gray code is :") print(gray_code) print("Binary code of", gray_code, "is", gray_to_binary(gray_code))
The gray code is : 01101001 Binary code of 01101001 is 01001110
A method named ‘flip_num’ is defined that checks to see if the number is 0.
If it is 0, it returns 1, else it returns 0.
Another method named ‘gray_to_binary’ is defined that takes a gray code as a parameter.
It iterates over the numbers in gray code, and stores the value in binary number’s index.
If the number is not 0, then the ‘flip_num’ method is called, and the number is changed to 1.
A binary number is defined, and the method is called by passing this value.
The output is displayed on the console.
- Python Program to Convert Binary to Gray Code
- 8085 program to convert gray to binary
- Conversion of Binary to Gray Code
- Conversion of Gray Code to Binary
- 8085 program to convert binary numbers to gray
- Binary to Gray code using recursion in C program
- Program to convert gray code for a given number in python
- 8086 program to convert binary to Grey code
- 8085 code to convert binary number to ASCII code
- What is Gray code?
- Gray Code in C++
- Python program to convert floating to binary
- Python program to convert decimal to binary number
- Convert decimal to binary number in Python program
- Python Program to Generate Gray Codes using Recursion