繁体   English   中英

如何在JAVA中打印int的ASCII值

[英]How to print ASCII value of an int in JAVA

我在互联网上搜索过这个,但找不到精确的解决方案,我发现的一个可能的解决方案是将整数作为字符串读取,并使用 charAt 方法,然后将字符转换为 int 以打印 ascii 值。

但是除了上述方法之外,还有其他可能的方法吗?

int a=sc.nextInt();
//code to convert it into its equivalent ASCII value.

例如,考虑读取的整数为1,现在我想在屏幕上打印ASCII值1,即49

我假设你正在寻找这个:

System.out.print((char)a);

做到这一点的简单方法是:


对于整个字符串

public class ConvertToAscii{ public static void main(String args[]){ String number = "1234"; int []arr = new int[number.length()]; System.out.println("THe asscii value of each character is: "); for(int i=0;i<arr.length;i++){ arr[i] = number.charAt(i); // assign the integer value of character ie ascii System.out.print(" "+arr[i]); } } }


对于单个字符: String number="123"; asciiValue = (int) number.charAt(0)//it coverts the character at 0 position to ascii value String number="123"; asciiValue = (int) number.charAt(0)//it coverts the character at 0 position to ascii value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM