简体   繁体   English

将String转换为char []-Java

[英]Converting String to char[] - Java

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 ie g[0] = b[0] as a result. 因此,我想出了下面给出的代码,它不能正常工作,它只能给我存储在开头的单个数字,即g [0] = b [0]。

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 谢谢

    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;
}

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... 一个int是一个整数数值,可以包含正数和负数:-3,-2,-1、0、1、2、3 ...

A char is still a numeric value, but represented as letters: 'a'(97), 'b'(98), 'c'(99)... char仍然是一个数字值,但用字母表示:'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). 我知道您已经知道这一点,因为您已经足够小心地计算两个字符的总和并将其标准化后减去2 *'0'(= 96)。 Good. 好。

But you must notice that every number included in your code is implicitly an int . 但是您必须注意, 代码中包含的每个数字都隐式地是一个int Now, realise that you are mixing ints and chars in several lines: 现在,意识到您正在将intschars混合在几行中:

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'; 首先,将您的数据标准化并将其存储到临时int变量中: int digit0=b[i - 1]-'0'; int digit1=b[i]-'0'; 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; } 执行计算并将其存储到一个临时的int变量中: int result=digit0 + digit1; if (result==2) { result=0; } 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'); 最后,将结果反规范化并将其存储到确定的输出变量中: 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. 最后,还必须控制数组的长度:如果知道输入数组的长度是多少,则应使用相同的长度预调整输出数组的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM