Java char数组转int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2683324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:46:58  来源:igfitidea点击:

Java char array to int

java

提问by Kyle

Is it possible to convert a char[]array containing numbers into an int?

是否可以将char[]包含数字的数组转换为int?

回答by Joachim Sauer

Does the char[]contain the unicode characters making up the digits of the number? In that case simply create a String from the char[]and use Integer.parseInt:

是否char[]包含组成数字数字的 unicode 字符?在这种情况下,只需从 中创建一个字符串char[]并使用 Integer.parseInt:

char[] digits = { '1', '2', '3' };
int number = Integer.parseInt(new String(digits));

回答by Nikita Koksharov

Another way with more performance:

另一种具有更高性能的方法:

char[] digits = { '1', '2', '3' };

int result = 0;
for (int i = 0; i < chars.length; i++) {
   int digit = ((int)chars[i] & 0xF);
   for (int j = 0; j < chars.length-1-i; j++) {
        digit *= 10;
   }
   result += digit;
}

回答by Thalur

Even more performance and cleaner code (and no need to allocate a new String object):

更高的性能和更简洁的代码(无需分配新的 String 对象):

int charArrayToInt(char []data,int start,int end) throws NumberFormatException
{
    int result = 0;
    for (int i = start; i < end; i++)
    {
        int digit = (int)data[i] - (int)'0';
        if ((digit < 0) || (digit > 9)) throw new NumberFormatException();
        result *= 10;
        result += digit;
    }
    return result;
}

回答by Ajay Rathore

char[] digits = { '0', '1', '2' };
int number = Integer.parseInt(String.valueOf(digits));

This also works.

这也有效。

回答by Lakmal

You can use like this for get one by one to int

您可以像这样使用一一到 int

char[] chars = { '0', '1', '2' };
int y=0;
for (int i = 0; i < chars.length; i++) {
    y = Integer.parseInt(String.valueOf(chars[i]));
    System.out.println(y);
}

回答by NAITIK GADA

Well, you can try even this

好吧,你甚至可以试试这个

    char c[] = {'1','2','3'};
    int total=0,x=0,m=1;
    for(int i=c.length-1;i>=0;i--)
    {
        x = c[i] - '0';
        x = x*m;
        m *= 10;
        total += x;
    }
    System.out.println("Integer is " + total);

回答by Nuwan Samarasinghe

It is possible to convert a char[]array containing numbers into an int. You can use following different implementation to convert array.

可以将char[]包含数字的数组转换为int. 您可以使用以下不同的实现来转换数组。

  1. Using Integer.parseInt

    public static int charArrayToInteger(char[] array){
        String arr = new String(array);
        int number = Integer.parseInt(arr);
    
        return number;
    }
    
  2. Without Integer.parseInt

    public static int charArrayToInt(char[] array){
        int result = 0;
        int length = array.length - 1;
    
        for (int i = 0; i <= length; i++)
        {
             int digit = array[i] - '0'; //we don't want to cast by using (int)
             result *= 10;
             result += digit;
        }
        return result;
    }
    
  1. 使用 Integer.parseInt

    public static int charArrayToInteger(char[] array){
        String arr = new String(array);
        int number = Integer.parseInt(arr);
    
        return number;
    }
    
  2. 没有 Integer.parseInt

    public static int charArrayToInt(char[] array){
        int result = 0;
        int length = array.length - 1;
    
        for (int i = 0; i <= length; i++)
        {
             int digit = array[i] - '0'; //we don't want to cast by using (int)
             result *= 10;
             result += digit;
        }
        return result;
    }
    

Example:

例子:

 public static void main(String []args){
    char[] array = {'1', '2', '3', '4', '5'};
    int result = 0;

    result = charArrayToInteger(array);
    System.out.println("Using Integer.parseInt: " + result);

    result = charArrayToInt(array);
    System.out.println("Without Integer.parseInt: " + result);

 }

Output:

输出:

Using Integer.parseInt: 12345
Without Integer.parseInt: 12345