gpt4 book ai didi

java - char(int) 和 int 的区别

转载 作者:行者123 更新时间:2023-12-02 07:21:08 26 4
gpt4 key购买 nike

我有一段代码,它从传感器接收值(通过使用 rxtx 的串行端口)并显示它。奇怪的是,下面的代码

int value = in.read();
System.out.print((char) value);

输出所需的值:

RXTX Warning:  Removing stale lock file. /var/lock/LK.005.018.009
20
27
29
26
21

但是当我将上面的代码更改如下时:

int value = in.read();
System.out.print("The value is"+(char) value);

输出变为:

RXTX Warning:  Removing stale lock file. /var/lock/LK.005.018.009
The value is2The value is6The value is
The value is2The value is2The value is

可以看出,整数 split 了。我想了半天都想不通?

有没有办法可以将控制台值保存为整数,因为我将来会使用这个值。

最佳答案

As it can be seen, the integer splits. For quite a while, I am unable to figure it out?

您不是在读取整数,而是在读取字符编码为“ASCII”的字节

Is there a way where I can save the console value into an integer, as I would be using this value in the future?

最简单的方法是使用扫描仪

Scanner scanner = new Scanner(in);
while (scanner.hasNextInt()) {
// read bytes up the next whitespace, parse as a int
int n = scanner.nextInt();

关于java - char(int) 和 int 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14212548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com