如何将ASCII转换为int?

时间:2011-04-19 10:22:05

标签: java

我得到一个ASCII值,我想把它转换成一个整数,因为我知道它是一个整数ASCII值。

int a=53; 

这是5的ASCII值。我怎样才能将它转换为整数?

8 个答案:

答案 0 :(得分:17)

int asciiValue = 53;
int numericValue = Character.getNumericValue(asciiValue);

System.out.println(numericValue);

答案 1 :(得分:6)

答案 2 :(得分:0)

使用Integer.parseInt(String)或如果只使用一个字符:int n = (int) (_char - '0')

答案 3 :(得分:0)

如果你确定它是一个int,你可以使用Integer.parseInt();

答案 4 :(得分:0)

如果可以保证字符串包含整数:

 Integer.parseInt(String)

答案 5 :(得分:0)

使用Integer.parseInt()静态方法:

Oracle Docs Integer.parseInt()

答案 6 :(得分:0)

使用:

try
{      
  int i = Integer.parseInt(StringValue);
}
catch (NumberFormatException nfe)
{
  System.out.println("NumberFormatException: " + nfe.getMessage());
}

答案 7 :(得分:0)

您的意思是单个字符还是字符串?

char ch = 
int n = Character.forDigit(ch, 10);

int n = ch - '0';
相关问题