(对我来说)字符数组中无法解释的值

发布于 2024-12-06 18:31:07 字数 222 浏览 0 评论 0原文

我被一些我真的不明白的事情困住了,我希望这里有人能明白。

谁能解释一下为什么“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:
problem reading value in character array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

胡渣熟男 2024-12-13 18:31:07

数组中的字符是“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 to char instead of int, 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.

迷迭香的记忆 2024-12-13 18:31:07

因为 51 是数字 3 的 ASCII 值。

Because 51 is the ASCII value for the numeral 3.

蓝色星空 2024-12-13 18:31:07

字符 '3' 对应于整数值 51(它是 ascii/unicode 值)。

The character '3' corresponds to an integer value of 51 (it's ascii/unicode value).

迷离° 2024-12-13 18:31:07

第一个数字是3,它的ASCII码是51。

The first digit is 3 and its ASCII code is 51.

邮友 2024-12-13 18:31:07

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'.

奶气 2024-12-13 18:31:07

51 是 3 的 ASCII 代码,因为 expression 是一个字符数组,而 nextOne 是一个 int,因此您必须将 char 转换为一个整数

51 is the ASCII code for three, since expression is an array of chars, and nextOne is an int, you have to convert the char to an int

墨落成白 2024-12-13 18:31:07

数字 3 的 ASCII 码是 51。以下表达式将在控制台中打印 "true"

System.out.println('3' == (char) 51);

有关详细信息,请查看 ASCII 表

The ASCII code of the number 3 is 51. The following expression will print "true" in the console.

System.out.println('3' == (char) 51);

Have a look at an ASCII table for more information.

差↓一点笑了 2024-12-13 18:31:07

如果您想要数字,您会看到字符“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

神经大条 2024-12-13 18:31:07

我做到了!现在它可以工作了,没有你们我就不可能做到这一点。
然而!有趣的是,你们所有人都为我提出了选择,但不是(在我看来)最终的选择。你们告诉我,存储的整数是 3 的 ASCII 代码,而不是实际的整数 3。这让我想起了 Character 类中的一个方法,getNumericValue(char)。使用此方法,我只是从 char 数组中检索整数 3,而无需自行转换。

nextNumber = nextNumber * 10 + Character.getNumericValue(expression[k]);

感谢@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.

nextNumber = nextNumber * 10 + Character.getNumericValue(expression[k]);

Thanks @howard for editing the link into the uploaded image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文