Java如何打印ASCII码

1 Java打印ASCII码的方式

美国信息交换标准代码的ASCII缩写。它是一个7位字符集,包含128个(0到127)个字符。它代表一个字符的数值。例如,ASCII值的 a 是65。

在本节中,我们将学习如何通过Java程序打印ASCII值或代码。

在Java中,有两种打印ASCII值的方法:

将变量分配给int变量

使用类型转换

2 方式一:将变量分配给int变量

要打印字符的ASCII值,我们不需要使用任何方法或类。Java在内部将字符值转换为ASCII值。

让我们通过Java程序找到字符的ASCII值。

在下面的程序中,我们分别在ch1和ch2变量中分配了两个字符a和b。为了找到a和b的ASCII值,我们分别将ch1和ch2变量分配给了整数变量asciivalue1和asciivalue2。最后,我们打印了变量asciivalue1和asciivalue2,其中存储了字符的ASCII值。

PrintAsciiValueExample1.java:

/**

* 一点教程网: http://www.yiidian

*/

public class PrintAsciiValueExample1

{

public static void main(String[] args)

{

// character whose ASCII value to be found

char ch1 = 'a';

char ch2 = 'b';

// variable that stores the integer value of the character

int asciivalue1 = ch1;

int asciivalue2 = ch2;

System.out.println("The ASCII value of " + ch1 + " is: " + asciivalue1);

System.out.println("The ASCII value of " + ch2 + " is: " + asciivalue2);

}

}

输出结果为:

The ASCII value of a is: 97

The ASCII value of b is: 98

编写上述程序的另一种方法是:

PrintAsciiValueExample2.java:

/**

* 一点教程网: http://www.yiidian

*/

public class PrintAsciiValueExample2

{

public static void main(String[] String)

{

int ch1 = 'a';

int ch2 = 'b';

System.out.println("The ASCII value of a is: "+ch1);

System.out.println("The ASCII value of b is: "+ch2);

}

}

输出结果为:

The ASCII value of a is: 97

The ASCII value of b is: 98

同样,我们可以打印其他字符(A,B,C,…。,Z)和符号(!,@,$,*等)的ASCII值。

3 方式二:使用类型转换

类型转换是一种将变量转换为另一种数据类型的方法。

在下面的程序中,我们声明了两个char类型的字符ch1和ch2 ,分别具有字符a和b。在接下来的两行中,我们使用(int)将char类型转换为int类型。执行这些两行后,变量CH1和CH2被转换为int变量ascii1和ASCII2分别。

最后,我们已经打印的可变ascii1和ASCII2在其中存储了字符的ASCII值。

PrintAsciiValueExample3.java:

/**

* 一点教程网: http://www.yiidian

*/

public class PrintAsciiValueExample3

{

public static void main(String[] args)

{

//characters whose ASCII value to be found

char ch1 = 'a';

char ch2 = 'b';

//casting or converting a charter into int type

int ascii1 = (int) ch1;

int ascii2 = (int) ch2;

System.out.println("The ASCII value of " + ch1 + " is: " + ascii1);

System.out.println("The ASCII value of " + ch1 + " is: " + ascii2);

}

}

输出结果为:

The ASCII value of a is: 97

The ASCII value of b is: 98

如果我们不想分配字符,我们也可以从用户那里获取字符。

PrintAsciiValueExample4.java:

/**

* 一点教程网: http://www.yiidian

*/

import java.util.Scanner;

public class PrintAsciiValueExample4

{

public static void main(String args[])

{

System.out.print("Enter a character: ");

Scanner sc = new Scanner(System.in);

char chr = sc.next().charAt(0);

int asciiValue = chr;

System.out.println("ASCII value of " +chr+ " is: "+asciiValue);

}

}

输出结果为1:

Enter a character: P

ASCII value of P is: 80

输出结果为2:

Enter a character: G

ASCII value of G is: 71

下面的程序打印所有字符的ASCII值(0到255)。在输出中,我们显示了一些值。

AsciiValueOfAllChracters.java:

/**

* 一点教程网: http://www.yiidian

*/

public class AsciiValueOfAllChracters

{

public static void main(String[] args)

{

for(int i = 0; i <= 255; i++)

{

System.out.println(" The ASCII value of " + (char)i + " = " + i);

}

}

}

输出结果为:

如果要打印所有字母(A到Z)的ASCII值,可以在循环中设置值并打印。

AsciiValueAtoZ.java:

/**

* 一点教程网: http://www.yiidian

*/

public class AsciiValueAtoZ

{

public static void main(String[] args)

{

for(int i = 65; i <= 90; i++)

{

System.out.println(" The ASCII value of " + (char)i + " = " + i);

}

}

}

输出结果为:

同样,我们可以通过更改上面代码中的循环,将a的ASCII值打印到z。

for(int i = 97; i <= 122; i++)

更多推荐

java如何输出ascll码_Java如何打印ASCII码