問(wèn)題描述
誰(shuí)能給我解釋一下這里發(fā)生了什么:
Can someone please explain to me what is going on here:
char c = '+';
int i = (int)c;
System.out.println("i: " + i + " ch: " + Character.getNumericValue(c));
這將打印 i: 43 ch:-1
.這是否意味著我必須依靠原始轉(zhuǎn)換將 char
轉(zhuǎn)換為 int
?那么如何將 Character
轉(zhuǎn)換為 Integer
?
This prints i: 43 ch:-1
. Does that mean I have to rely on primitive conversions to convert char
to int
? So how can I convert a Character
to Integer
?
是的,我知道如果 Character.getNumericValue
不是數(shù)值,那么它會(huì)返回 -1
,這對(duì)我來(lái)說(shuō)很有意義.問(wèn)題是:為什么進(jìn)行原始轉(zhuǎn)換會(huì)返回 43
?
Yes I know Character.getNumericValue
returns -1
if it is not a numeric value and that makes sense to me. The question is: why does doing primitive conversions return 43
?
Edit2: 43
是 +
的 ASCII,但我希望轉(zhuǎn)換不會(huì)像 getNumericValue代碼> 沒(méi)有成功.否則這意味著有兩種語(yǔ)義等效的方式來(lái)執(zhí)行相同的操作但結(jié)果不同?
43
is the ASCII for +
, but I would expect the cast to not succeed just like getNumericValue
did not succeed. Otherwise that means there are two semantic equivalent ways to perform the same operation but with different results?
推薦答案
Character.getNumericValue(c)
java.lang.Character.getNumericValue(char ch)
返回指定 Unicode 字符所代表的 int
值.例如,字符 'u216C'
(羅馬數(shù)字 50)將返回一個(gè)值為 50 的 int.
The java.lang.Character.getNumericValue(char ch)
returns the int
value that the specified Unicode character represents. For example, the character 'u216C'
(the roman numeral fifty) will return an int with a value of 50.
字母 AZ 大寫(xiě) ('u0041' 到 'u005A')
、小寫(xiě) ('u0061' 到 'u007A')
和完整寬度變體 ('uFF21' 到 'uFF3A' 和 'uFF41' 到 'uFF5A')
形式的數(shù)值從 10 到 35.這與 Unicode 規(guī)范無(wú)關(guān),它不為這些 char 值分配數(shù)值.
The letters A-Z in their uppercase ('u0041' through 'u005A')
, lowercase ('u0061' through 'u007A')
, and full width variant ('uFF21' through 'uFF3A' and 'uFF41' through 'uFF5A')
forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
此方法返回字符的數(shù)值,作為非負(fù)整數(shù)值;
This method returns the numeric value of the character, as a nonnegative int value;
-2 如果字符的數(shù)值不是非負(fù)整數(shù);
-2 if the character has a numeric value that is not a nonnegative integer;
-1 如果字符沒(méi)有數(shù)值.
-1 if the character has no numeric value.
這里是鏈接.
這篇關(guān)于在Java中將字符轉(zhuǎn)換為整數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!