package com;
import java.util.Scanner;
import java.util.Stack;
public class GrayCode {
/**
* @param args
*/
int index=0;
int[] binary = new int[10];
/*
* convert from decimal to binary and print
*/
public void convertBinary(int dVal){
Stack<Integer> stack = new Stack<Integer>();
while(dVal!=0){
int d= dVal%2;
stack.push(d);
dVal=dVal/2;
}
while(!(stack.empty())){
int p = stack.pop();
binary[index++]=p;
}
System.out.print("Gray Code :");
for(int i=0;i<=index-1;i++){ // Print Gray Code
System.out.print(binary[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter Binary value :");
Scanner scr = new Scanner(System.in);
String bValue = scr.nextLine();
int dVal = Integer.parseInt(bValue, 2); // Convert from binary string to Decimal
System.out.println("Decimal value of the entered binary value : " +dVal);
dVal=dVal^(dVal>>1); // Algo to Convert into gray code
System.out.println("Decimal value converted into equivalent gray decimal value : "+dVal);
GrayCode gray = new GrayCode();
gray.convertBinary(dVal); // convert Gray code into binary
//System.out.println(Integer.toBinaryString(124));
}
}
No comments:
Post a Comment