如何将ASCII转换为int?

问题描述 投票:13回答:8

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

int a=53; 

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

java
8个回答
18
投票
int asciiValue = 53;
int numericValue = Character.getNumericValue(asciiValue);

System.out.println(numericValue);


0
投票

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


0
投票

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

 Integer.parseInt(String)

0
投票

使用:

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

0
投票

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

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

要么

int n = ch - '0';

0
投票

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


0
投票

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

Oracle Docs Integer.parseInt()

© www.soinside.com 2019 - 2024. All rights reserved.