如何在Java中将char转换为int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46343616/
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-12 02:19:17  来源:igfitidea点击:

How can I convert a char to int in Java?

javachartype-conversionint

提问by Haim Lvov

(I'm new at Java programming)

(我是 Java 编程新手)

I have for example:

我有例如:

char x = '9';

and I need to get the number in the apostrophes, the digit 9 itself. I tried to do the following,

我需要得到撇号中的数字,即数字 9 本身。我尝试执行以下操作,

char x = 9;
int y = (int)(x);

but it didn't work.

但它没有用。

So what should I do to get the digit in the apostrophes?

那么我应该怎么做才能得到撇号中的数字呢?

采纳答案by khelwood

The ASCII table is arranged so that the value of the character '9'is nine greater than the value of '0'; the value of the character '8'is eight greater than the value of '0'; and so on.

ASCII 表的排列使得字符'9'的值比 的值大九个'0';字符'8'的值比 的值大八'0';等等。

So you can get the int value of a decimal digit char by subtracting '0'.

因此,您可以通过减去'0'.

char x = '9';
int y = x - '0'; // gives the int value 9

回答by azro

I you have the char '9', it will store its ASCII code, so to get the int value, you have 2 ways

我你有char '9',它会存储它的 ASCII 码,所以要获得 int 值,你有 2 种方法

char x = '9';
int y = Character.getNumericValue(x);   //use a existing function
System.out.println(y + " " + (y + 1));  // 9  10

or

或者

char x = '9';
int y = x - '0';                        // substract '0' code to get the difference
System.out.println(y + " " + (y + 1));  // 9  10


And it fact, this works also :

事实上,这也有效:

char x = 9;
System.out.println(">" + x + "<");     //>  < prints a horizontal tab
int y = (int) x;
System.out.println(y + " " + (y + 1)); //9 10

You store the 9code, which corresponds to a horizontal tab(you can see when print as String, bu you can also use it as intas you see above

您存储9对应于 a的代码horizontal tab(您可以在 print as 时看到String,但是您也可以像int上面看到的那样使用它

回答by Amol Raje

If you want to get the ASCII value of a character, or just convert it into an int, you need to cast from a char to an int.

如果要获取字符的 ASCII 值,或者只是将其转换为 int,则需要从 char 转换为 int。

What's casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here's a brief example.

什么是铸造?强制转换是指我们将一种原始数据类型或类显式转换为另一种。这是一个简短的例子。

public class char_to_int
{
  public static void main(String args[])
  {
       char myChar = 'a';
       int  i = (int) myChar; // cast from a char to an int
       System.out.println ("ASCII value - " + i);
  }

In this example, we have a character ('a'), and we cast it to an integer. Printing this integer out will give us the ASCII value of 'a'.

在这个例子中,我们有一个字符 ('a'),我们将它转​​换为一个整数。打印这个整数会给我们 'a' 的 ASCII 值。

回答by 2787184

You can use static methods from Character class to get Numeric value from char.

您可以使用 Character 类中的静态方法从 char 中获取数值。

char x = '9';

if (Character.isDigit(x)) { // Determines if the specified character is a digit.
    int y = Character.getNumericValue(x); //Returns the int value that the 
                                          //specified Unicode character represents.
    System.out.println(y);
}