问题描述
为什么强制转换(int)可以正确地将符号的字符转换为int,但是Character.getNumericValue('someSymbolCharValue');"不能吗?
Why is it that casting (int) can convert chars of a symbol to int properly, but "Character.getNumericValue('someSymbolCharValue');" can't?
例如"Character.getNumericValue('?');" 将返回 -1,即使 char 可以用 "(int) '?'" 表示为 int返回 63
E.g. "Character.getNumericValue('?');" will return -1 even though the char can be presented as int with "(int) '?'" where it will return 63
推荐答案
一个字符的数值"不是它的 ASCII/Unicode 索引值.Character.getNumericValue
方法 将尝试通过应用字符的数字含义将 char
转换为 int
:
A character's "numeric value" is not its ASCII/Unicode index value. The Character.getNumericValue
method will attempt to convert the char
to an int
by applying the character's numeric meaning:
返回指定的 Unicode 字符表示的 int 值.例如,字符 'u216C'(罗马数字 50)将返回一个值为 50 的 int.大写字母 AZ('u0041' 到 'u005A')、小写字母('u0061' 到 'u007A')和全角变体('uFF21' 到 'uFF3A' 和 'uFF41'通过 'uFF5A') 形式具有从 10 到 35 的数值.这与 Unicode 规范无关,Unicode 规范不会为这些 char 值分配数值.
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. 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.
如果字符没有数值,则返回-1.如果字符具有不能表示为非负整数的数值(例如,小数值),则返回 -2.
If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.
预计 getNumericValue
不会同意将 char
强制转换为 int
.
It is expected that getNumericValue
will not agree with casting the char
to an int
.
这篇关于转换字符(铸造与 .getNumericValue)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!