0

This is my function of converting binary to Gray Code.

public void convert(String bin)
{
    char[] b = bin.toCharArray();
    char[] g = new char[100];
    System.out.print(g[0]);
    for(int i=1;i<b.length;i++)
    {
        System.out.print(g[i] = (b[i-1] + b[i]) - 96);
        if(g[i] == '2')
        {
            System.out.print(0);
            i++;
            continue;
        }
        System.out.print(g[i] - 0);
    }
}

I have above function which works perfectly fine but I want to return the converted string from this function. So I come up with the code given below, which is not working fine and it only give me the single digit which I store in starting i.e. g[0] = b[0] as a result.

public String convert(String bin)
{
    char[] b = bin.toCharArray();
    char[] g = new char[100];
    g[0] = b[0];
    for(int i=1;i<b.length;i++)
    {
        g[i] = (char)((b[i-1] + b[i]) - 96);
        if(g[i] == '2')
        {
            g[i] = 0;
            i++;
            continue;
        }
        g[i] = g[i] - 0;
    }
    String gray = String.valueOf(g);
    return gray;
}

How can I do this so that it give me the result which I want.

Thanks

7
  • I think you are looking for new String(g). Commented Oct 28, 2015 at 15:25
  • @RealSkeptic Sorry I did'nt get you Commented Oct 28, 2015 at 15:26
  • g[i] = g[i] - 0; has no effect and g[i] = 0; is probably wrong Commented Oct 28, 2015 at 15:26
  • @RealSkeptic String valueOf(char g[]) translates to new String(g) Commented Oct 28, 2015 at 15:27
  • @wero this is what I am saying that how can i do this g[i] = g[i] - 0 without any error? Commented Oct 28, 2015 at 15:28

2 Answers 2

1
    public static String convert(String bin)
    {

        //System.out.println( "The Gray Equivalent Is: ");
        char[] b = bin.toCharArray();
        StringBuilder g = new StringBuilder(); // Use StringBuilder
        g.append(b[0]);
        //System.out.print(g[0]);
        for(int i=1;i<b.length;i++)
        {
            int val = (int)b[i-1] ^(int) b[i]; // use exclusive-or( ^ ) 
            if(val == '2')
            {
                //System.out.print(0);
                g.append(0);
                i++;
                continue;
        }

        //System.out.print(g[i] - 0);
        g.append(val);
    }

    String gray = String.valueOf(g);
    return gray;
}
Sign up to request clarification or add additional context in comments.

2 Comments

1) Please only post formatted code, so that it is more readable. 2) Please comment your code, try to explain what you did, why and how this is a good solution. Also explain what was wrong or suboptimal with OPs attempts.
Thanks Man. Works perfectly fine
0

I see what you want to achieve. But you are mistaking the integer values with the character values. Look:

An int is a integer numeric value, which can contain positive and negative numbers: -3, -2, -1, 0, 1, 2, 3...

A char is still a numeric value, but represented as letters: 'a'(97), 'b'(98), 'c'(99)...

I know you already know this, because you have been careful enough to compute the sum of two chars and normalize it substracting 2*'0' (=96). Good.

But you must notice that every number included in your code is implicitly an int. Now, realise that you are mixing ints and chars in several lines:

if(g[i] == '2')
g[i] = 0;
g[i] = g[i] - 0;

My suggestion: Follow an order:

  1. Fist, normalize your data and store them into temporary int variables: int digit0=b[i - 1]-'0'; int digit1=b[i]-'0';
  2. Perform the calculations and store it into a temporary int variable: int result=digit0 + digit1; if (result==2) { result=0; }
  3. Last, de-normalize the result and store it into the definitive output variable: g[i]=(char)(result + '0'); In the last place, you must also control the length of the arrays: If you know what is the length of the input array, you should pre-size the output array with the same length.

1 Comment

Thanks for your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.