Posted on by Kalkicode
Code Conversion

# Conversion from gray to binary code

Gray code is a binary numeral system where two consecutive values differ by only one bit. This type of code is widely used in various applications, such as error detection and correction, digital communications, and analog-to-digital conversion. In this article, we will explore how to convert Gray code to binary code using a simple algorithm.

## Problem Statement

The problem is to take a Gray code as input and convert it to its corresponding binary representation. The input is a string representing the Gray code, where each character can be either '0' or '1'.

## Example

Let's consider the Gray code "01001111" as an example. To convert this Gray code to binary, we follow the algorithm described below.

## Pseudocode

``````
grayToBinary(gray):
n = length(gray)
binary = gray
for i = 1 to n-1:
if gray[i] == '0':
binary = binary + binary[i-1]
else if binary[i-1] == '0':
binary = binary + '1'
else:
binary = binary + '0'
print "Gray: " + gray
print "Binary: " + binary
```
```

## Algorithm Explanation

The algorithm takes the Gray code as input and converts it to binary using a loop. It initializes the binary variable with the first character of the Gray code. Then, it iterates through the remaining characters of the Gray code.

For each character, it checks the following conditions:

• If the current character in the Gray code is '0', it appends the same value as the previous binary bit to the binary string.
• If the previous binary bit is '0', it appends '1' to the binary string.
• Otherwise, it appends '0' to the binary string.

After the loop, it prints the original Gray code and the converted binary code.

## Program Solution

``````/*
Java Program
Conversion from gray to binary code
*/
public class Conversion
{

// Convert given gray code to binary number
public void grayToBinary(String gray)
{
// Get the length of given number
int n = gray.length();
// Used to collect result
String binary = "";
// Set first most significant bit
binary = ""+gray.charAt(0);
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (gray.charAt(i)== '0')
{

binary += binary.charAt(i-1);
}
else if(binary.charAt(i-1)=='0')
{

binary += "1";
}
else
{
binary += "0";
}
}

// Display calculated result
System.out.print(" Gray : " + gray );
System.out.print("\n Binary : " + binary + " \n\n");

}
public static void main(String[] args)
{

}
}``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````// C Program
// Conversion from gray to binary code
#include <stdio.h>

#include <string.h>

// Convert given gray code to binary number
void grayToBinary(const char *gray)
{
// Get the length of given gray code
int n = strlen(gray);
// Used to collect result
char binary[n + 1];
// Set first most significant bit
binary = gray;
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (gray[i] == '0')
{
binary[i] = binary[i - 1];
}
else if (binary[i - 1] == '0')
{
binary[i] = '1';
}
else
{
binary[i] = '0';
}
}
binary[n] = '\0';
// Display calculated result
printf(" Gray : %s", gray);
printf("\n Binary : %s\n\n", binary);
}
int main()
{
// Test Case
grayToBinary("01001111");
grayToBinary("1111");
return 0;
}``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````// Include header file
#include <iostream>
#include <string.h>

using namespace std;
/*
C++ Program
Conversion from gray to binary code
*/
class Conversion
{
public:
// Convert given gray code to binary number
void grayToBinary(string gray)
{
// Get the length of given number
int n = gray.size();
// Used to collect result
string binary = "";
// Set first most significant bit
binary = gray;
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (gray[i] == '0')
{
binary += binary[i - 1];
}
else if (binary[i - 1] == '0')
{
binary += "1";
}
else
{
binary += "0";
}
}
// Display calculated result
cout << " Gray : " << gray;
cout << "\n Binary : " << binary << " \n\n";
}
};
int main()
{
return 0;
}``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````// Include namespace system
using System;
/*
C# Program
Conversion from gray to binary code
*/
public class Conversion
{
// Convert given gray code to binary number
public void grayToBinary(String gray)
{
// Get the length of given number
int n = gray.Length;
// Used to collect result
String binary = "";
// Set first most significant bit
binary = "" + gray;
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (gray[i] == '0')
{
binary += binary[i - 1];
}
else if (binary[i - 1] == '0')
{
binary += "1";
}
else
{
binary += "0";
}
}
// Display calculated result
Console.Write(" Gray : " + gray);
Console.Write("\n Binary : " + binary + " \n\n");
}
public static void Main(String[] args)
{
}
}``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````<?php
/*
Php Program
Conversion from gray to binary code
*/
class Conversion
{
// Convert given gray code to binary number
public	function grayToBinary(\$gray)
{
// Get the length of given number
\$n = strlen(\$gray);
// Used to collect result
\$binary = "";
// Set first most significant bit
\$binary = "". \$gray;
// Execute loop through by length
for (\$i = 1; \$i < \$n; ++\$i)
{
if (\$gray[\$i] == '0')
{
\$binary .= \$binary[\$i - 1];
}
else if (\$binary[\$i - 1] == '0')
{
\$binary .= "1";
}
else
{
\$binary .= "0";
}
}
// Display calculated result
echo " Gray : ". \$gray;
echo "\n Binary : ". \$binary ." \n\n";
}
}

function main()
{
}
main();``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````/*
Node Js Program
Conversion from gray to binary code
*/
class Conversion
{
// Convert given gray code to binary number
grayToBinary(gray)
{
// Get the length of given number
var n = gray.length;
// Used to collect result
var binary = "";
// Set first most significant bit
binary = "" + gray;
// Execute loop through by length
for (var i = 1; i < n; ++i)
{
if (gray[i] == '0')
{
binary += binary[i - 1];
}
else if (binary[i - 1] == '0')
{
binary += "1";
}
else
{
binary += "0";
}
}
// Display calculated result
process.stdout.write(" Gray : " + gray);
process.stdout.write("\n Binary : " + binary + " \n\n");
}
}

function main()
{
}
main();``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````#   Python 3 Program
#   Conversion from gray to binary code

class Conversion :
#  Convert given gray code to binary number
def grayToBinary(self, gray) :
#  Get the length of given number
n = len(gray)
#  Used to collect result
binary = ""
#  Set first most significant bit
binary = gray
i = 1
#  Execute loop through by length
while (i < n) :
if (gray[i] == '0') :
binary += binary[i - 1]

elif(binary[i - 1] == '0') :
binary += "1"
else :
binary += "0"

i += 1

#  Display calculated result
print(" Gray : ", gray, end = "")
print("\n Binary : ", binary ," \n")

def main() :

if __name__ == "__main__": main()``````

#### Output

`````` Gray :  01001111
Binary :  01110101

Gray :  1111
Binary :  1010
``````
``````#   Ruby Program
#   Conversion from gray to binary code

class Conversion
#  Convert given gray code to binary number
def grayToBinary(gray)
#  Get the length of given number
n = gray.length()
#  Used to collect result
binary = ""
#  Set first most significant bit
binary =  gray
i = 1
#  Execute loop through by length
while (i < n)
if (gray[i] == '0')
binary += binary[i - 1]
elsif(binary[i - 1] == '0')
binary += "1"
else
binary += "0"
end

i += 1
end

#  Display calculated result
print(" Gray : ", gray)
print("\n Binary : ", binary ," \n\n")
end

end

def main()
end

main()``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010

``````
``````/*
Scala Program
Conversion from gray to binary code
*/
class Conversion
{
// Convert given gray code to binary number
def grayToBinary(gray: String): Unit = {
// Get the length of given number
var n: Int = gray.length();
// Used to collect result
var binary: String = "";
// Set first most significant bit
binary = "" + gray(0);
var i: Int = 1;
// Execute loop through by length
while (i < n)
{
if (gray(i) == '0')
{
binary += binary(i - 1);
}
else if (binary(i - 1) == '0')
{
binary += "1";
}
else
{
binary += "0";
}
i += 1;
}
// Display calculated result
print(" Gray : " + gray);
print("\n Binary : " + binary + " \n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Conversion = new Conversion();
}
}``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````
``````/*
Swift 4 Program
Conversion from gray to binary code
*/
class Conversion
{
// Convert given gray code to binary number
func grayToBinary(_ gray: String)
{
// Get the length of given number
let n: Int = gray.count;
// Used to collect result
var binary: String = "";
// Set first most significant bit
binary = String(Array(gray));
var i: Int = 1;
// Execute loop through by length
while (i < n)
{
if (Array(gray)[i] == "0")
{
binary += String(Array(binary)[i-1]);
}
else if (Array(binary)[i-1] == "0")
{
binary += "1";
}
else
{
binary += "0";
}
i += 1;
}
// Display calculated result
print(" Gray : ", gray, terminator: "");
print("\n Binary : ", binary ," \n");
}
}
func main()
{
}
main();``````

#### Output

`````` Gray :  01001111
Binary :  01110101

Gray :  1111
Binary :  1010
``````
``````/*
Kotlin Program
Conversion from gray to binary code
*/
class Conversion
{
// Convert given gray code to binary number
fun grayToBinary(gray: String): Unit
{
// Get the length of given number
var n: Int = gray.count();
// Used to collect result
// Set first most significant bit
var binary: String = "" + gray;
var i: Int = 1;
// Execute loop through by length
while (i < n)
{
if (gray[i] == '0')
{
binary += binary[i - 1];
}
else if (binary[i - 1] == '0')
{
binary += "1";
}
else
{
binary += "0";
}
i += 1;
}
// Display calculated result
print(" Gray : " + gray);
print("\n Binary : " + binary + " \n\n");
}
}
fun main(args: Array < String > ): Unit
{
}``````

#### Output

`````` Gray : 01001111
Binary : 01110101

Gray : 1111
Binary : 1010
``````

## Output Explanation

Using the provided example, let's see the step-by-step conversion:

``````
Gray: 01001111
Binary: 01110101
```
```

The Gray code "01001111" is converted to the binary code "01110101" using the algorithm we discussed.

Similarly, let's consider the Gray code "1111" as another example:

``````
Gray: 1111
Binary: 1010
```
```

The Gray code "1111" is converted to the binary code "1010" following the same algorithm.

## Time Complexity

The time complexity of the conversion algorithm is O(n), where n is the length of the input Gray code. This is because we iterate through each character of the Gray code exactly once.

## Finally

In this article, we discussed the problem of converting Gray code to binary code. We explained the algorithm step-by-step and provided a suitable example for better understanding. The algorithm has a time complexity of O(n), where n is the length of the Gray code. By following the algorithm, we can accurately convert Gray codes to their binary representations. This conversion is important in various applications that involve binary data processing and manipulation.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post