(对我来说)字符数组中无法解释的值
我被一些我真的不明白的事情困住了,我希望这里有人能明白。
谁能解释一下为什么“nextOne”的值为 51?很明显,表达式数组中的索引 2 处是数字 3,那么为什么它在 nextOne 中存储 51 而不是 3?
这里是:
I'm stuck with something I really don't understand and I hope someone over here does.
Can anybody explain me why 'nextOne' holds the value 51? It's clear that at index 2 in the expression array is the number 3 so why does it store 51 in stead of 3 in nextOne?
Here it is:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
数组中的字符是“3”。它的 Unicode 值为 51 - 它是字符 U+0033。
如果将
nextOne
的类型更改为char
而不是int
,您将看到“3”。了解作为字符的数字与整数值之间的区别非常重要,并且了解隐藏在字符后面的数字;在 Java 中,它们基本上是 UTF-16 代码单元。
The character in the array is '3'. That has a Unicode value of 51 - it's character U+0033.
If you change the type of
nextOne
tochar
instead ofint
, you'll see '3' instead.It's important to understand the difference between digits as characters, and integer values - and also to understand the numbers which lurk behind characters; in Java they're UTF-16 code units, basically.
因为 51 是数字
3
的 ASCII 值。Because 51 is the ASCII value for the numeral
3
.字符
'3'
对应于整数值51
(它是 ascii/unicode 值)。The character
'3'
corresponds to an integer value of51
(it's ascii/unicode value).第一个数字是
3
,它的ASCII码是51。The first digit is
3
and its ASCII code is 51.nextOne 包含字符的值,在字符“3”的情况下,该值是 51。
如果您想要该字符表示的数字,则需要对其进行转换。对于 ASCII,可以通过减去字符“0”的值来完成。
nextOne contains the value of the character, which, in the case of the character '3' is 51.
If you want the digit represented by the character, you need to convert it. With ASCII, that can be done by subtracting the value of the character '0'.
51 是 3 的 ASCII 代码,因为
expression
是一个字符数组,而nextOne
是一个int
,因此您必须将 char 转换为一个整数51 is the ASCII code for three, since
expression
is an array of chars, andnextOne
is anint
, you have to convert the char to an int数字 3 的 ASCII 码是 51。以下表达式将在控制台中打印
"true"
。有关详细信息,请查看 ASCII 表。
The ASCII code of the number 3 is 51. The following expression will print
"true"
in the console.Have a look at an ASCII table for more information.
如果您想要数字,您会看到字符“3”的 ascii 代码,您需要减去
'0'
才能获得整数值you see the ascii code of the character '3' if you want the digit you need to subtract
'0'
to get the integer value我做到了!现在它可以工作了,没有你们我就不可能做到这一点。
然而!有趣的是,你们所有人都为我提出了选择,但不是(在我看来)最终的选择。你们告诉我,存储的整数是 3 的 ASCII 代码,而不是实际的整数 3。这让我想起了 Character 类中的一个方法,getNumericValue(char)。使用此方法,我只是从 char 数组中检索整数 3,而无需自行转换。
感谢@howard 将链接编辑到上传的图像中
I did it! It works now and I couldn't have done it without you guys.
However! Funny how all of you suggested options for me but not the (in my opinion) ultimate one. You guys told me the integer that was being stored is the ASCII code for 3, not the actual integer 3. That reminded me of a method in the Character class, getNumericValue(char). With this method I simply retrieved the integer 3 from the char array without converting myself.
Thanks @howard for editing the link into the uploaded image