2011-04-19 123 views
10

斐伊川我得到的ASCII值,我想將其轉換成一個Ineger因爲我知道它的整數ASCII值..將ascii轉換爲int?

int a=53 

這是5的ASCII值,我想轉換成整型

回答

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

System.out.println(numericValue); 
0

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

+0

0:第二示例將一個數字的字符。不需要強制轉換爲「(int)」。 – 2011-04-19 10:28:03

+0

你是完全正確的,讓我編輯 – dcn 2011-04-19 10:29:38

0

如果你能保證該字符串包含一個整數:

Integer.parseInt(String) 
0

用途:

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

+1嘗試捕獲 – 2011-04-19 19:10:06

0

你是指單個字符還是字符串?

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

int n = ch - '0';