ASCII 是用于将英文字符表示为数字的代码,英文字母的每个字母被分配一个从 0 到 127 的数字。例如,大写字母P的 ASCII 代码是 80。
在 Java 编程中,我们有两种方法可以找到一个字符的 ASCII 值 1)通过为int变量赋一个字符 2)通过将字符的类型转换为int
让我们编写一个程序来了解它是如何工作的:
示例:用于查找字符的 ASCII 代码的程序
public class Demo { public static void main(String[] args) { char ch = 'P'; int asciiCode = ch; // type casting char as int int asciiValue = (int)ch; System.out.println("ASCII value of "+ch+" is: " + asciiCode); System.out.println("ASCII value of "+ch+" is: " + asciiValue); }}输出:
ASCII value of P is: 80ASCII value of P is: 80
