1

Javaで数値のUnicodeを数値自体に変換するにはどうすればよいですか?

char c ='2';
int x =c;
// here x=unicode of 2, how can I put the 2 itself in x?
4

2 に答える 2

2

最も一般的な解決策は

int x = Character.digit(c, 10); // interpret c as a digit in base 10

最速の解決策は、悪い入力を処理するために何もしませんが、

int x = c - '0'; // subtracts the Unicode representation of '0' from c
于 2012-12-14T18:38:49.007 に答える
1

使用する

int x = Character.getNumericValue(c);

また

int x = Integer.valueOf(String.valueOf(c));

Integer.valueOfは他のintとStringsしか処理できないため、2番目のアプローチでは2つの変換が必要であることに注意してください。

于 2012-12-14T18:37:23.500 に答える