如何在Java中将整数转换为字符?

huangapple go评论66阅读模式
英文:

How to convert an integer to char in Java?

问题

我正在尝试在Java中获取整数的ASCII字符,但是在我尝试进行类型转换时它给我一个空字符。我正在执行:

int slowest = keyTimes.get(keyIndex).first;

其中,keyTimes是一个PairInts的列表,而PairInt包含两个整数,分别是first和second。然后我执行:

char ch = (char) slowest;

但是这并没有给我ASCII字符,并且输出是空白的。

英文:

I am trying to get the ASCII character of an integer in java, but it is giving me an empty char when I try to typecast it. I am doing:

int slowest = keyTimes.get(keyIndex).first;

where keyTimes is a List of PairInts and PairInt has 2 integers, first and second. I am then doing

char ch = (char) slowest; 

but this does not give me the ASCII character, and prints blank.

答案1

得分: 1

int a=65;
char c=(char)a;
System.out.println(a);

这将输出 "A"。如果给定的数字没有对应的 ASCII 字符,也会输出黑色。因此,请检查您提供的整数。

英文:
int a=65;  
char c=(char)a;
System.out.println(a);    

This will give "A" as output. Also give black output if there is no ASCII character of given number. So please check the integer that you are providing.

答案2

得分: 0

如果打印 ch 会产生空格,则 slowest 的值将为 32,因为空格的 ASCII 值为 32。
您可以在 https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html 查看 ASCII 到字符的映射。

英文:

If printing ch results in empty space, then slowest value would be 32 as ASCII value of space is 32.
You can check the ASCII to character mapping at https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

huangapple
  • 本文由 发表于 2020年9月9日 12:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63804987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定