转换字符(铸造与.getNumericValue) [英] Converting chars (Casting vs. .getNumericValue)

查看:93
本文介绍了转换字符(铸造与.getNumericValue)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么强制转换(int)可以将符号的字符正确转换为int,但是却是"Character.getNumericValue('someSymbolCharValue');";不能吗?

例如"Character.getNumericValue('?');" 将返回-1,即使char可以用"(int)'?'"表示为int时也是如此.返回63

解决方案

字符的数字值"不是其ASCII/Unicode索引值. Character.getNumericValue方法会尝试通过应用字符的数字含义将char转换为int:

返回指定的Unicode字符表示的int值.例如,字符"\ u216C"(罗马数字五十)将返回一个值为50的int. 字母AZ以大写字母('\ u0041'到'\ u005A'),小写字母('\ u0061'到'\ u007A')和全角变体('\ uFF21'到'\ uFF3A'和'\ uFF41'到'\ uFF5A'形式)的数字值为10到35.这独立于Unicode规范,Unicode规范没有为这些char值分配数字值.

如果字符没有数字值,则返回-1.如果字符的数字值不能表示为非负整数(例如,小数),则返回-2.

预计getNumericValue将不同意将char强制转换为int.

Why is it that casting (int) can convert chars of a symbol to int properly, but "Character.getNumericValue('someSymbolCharValue');" can't?

E.g. "Character.getNumericValue('?');" will return -1 even though the char can be presented as int with "(int) '?'" where it will return 63

解决方案

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:

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.

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.

It is expected that getNumericValue will not agree with casting the char to an int.

这篇关于转换字符(铸造与.getNumericValue)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆