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

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

相关文档推荐

quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)