转换字符(铸造与 .getNumericValue)

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

问题描述

为什么强制转换(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)