打印 ASCII 值的Java程序
ASCII 是代表美国信息交换标准代码的首字母缩写词。在 ASCII 中,特定的数值被赋予不同的字符和符号,供计算机存储和操作,并且在存储和操作电子设备时始终使用给定的 ASCII 数字的二进制值。因为在原始形式中不可能做到这一点。
方法:有4种方法可以打印特定字符的ASCII值或代码,下面列出了概念,然后是实现部分的Java示例。
- 使用蛮力法
- 使用类型转换方法
- 使用格式说明符方法
- 使用字节类方法
方法一:给int变量赋值
为了找到字符字符给一个新的整数类型变量。 Java会自动将该字符的 ASCII 值存储在新变量中。
实施:蛮力法
Java
// Java program to print ASCII Value of Character
// by assigning variable to integer
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to be computed
char ch = '}';
// Creating a new variable of type int
// and assigning the character value.
int ascii = ch;
/* Java stores the ascii value there itself*/
// Printing the ASCII value of above character
System.out.println("The ASCII value of " + ch
+ " is: " + ascii);
}
}
Java
// Java program to print ASCII Value of Character
// using type-casting
// Importing java generic libraries
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to be computed
char ch = '}';
// Typecasting the character to int and
// printing the same
System.out.println("The ASCII value of " + ch
+ " is: " + (int)ch);
}
}
Java
// Java program to print ASCII Value of Character
// using format specifier
// Importing format library
import java.util.Formatter;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to compute
char character = '}';
// Initializing the format specifier
Formatter formatSpecifier = new Formatter();
// Converting the character to integer and
// ASCII value is stored in the format specifier
formatSpecifier.format("%d", (int)character);
// Print the corresponding ASCII value
System.out.println(
"The ASCII value of the character ' "
+ character + " ' is " + formatSpecifier);
}
}
Java
// Java program to print ASCII Value of Character
// by generating bytes.
// Importing I/O library
import java.io.UnsupportedEncodingException;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Try block to check exception
try {
// Character is initiated as a string
String sp = "}";
// An array of byte type is ccreated
// by using getBytes method
byte[] bytes = sp.getBytes("US-ASCII");
/*This is the ASCII value of the character
/ present at the '0'th index of above string.*/
// Printing the element at '0'th index
// of array(bytes) using charAt() method
System.out.println("The ASCII value of "
+ sp.charAt(0) + " is "
+ bytes[0]);
}
// Catch block to handle exception
catch (UnsupportedEncodingException e) {
// Message printed foe exception
System.out.println("OOPs!!!UnsupportedEncodingException occurs.");
}
}
}
The ASCII value of } is: 125
方法 2:使用类型转换
Java中的类型转换是一种将变量转换为另一种数据类型的方法,这意味着持有另一种数据类型的值占用较少的字节。在这种方法中,字符在打印时是 char 类型到 int 类型的类型转换,它将打印字符的 ASCII 值。
Java
// Java program to print ASCII Value of Character
// using type-casting
// Importing java generic libraries
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to be computed
char ch = '}';
// Typecasting the character to int and
// printing the same
System.out.println("The ASCII value of " + ch
+ " is: " + (int)ch);
}
}
The ASCII value of } is: 125
Note: In above method 1 and method 2, both the methods are one type of typecasting. In method 1, typecasting is done automatically by the compiler. In method 2, typecasting it manually so the method 2 is much more efficient than method 1 as the compiler has to put lesser effort. Also, remember typecasting done automatically is called implicit typecasting and where it is done from the user end is called explicit typecasting
方法 3:使用格式说明符(更优)
在这种方法中,我们在格式说明符的帮助下生成给定字符的 ASCII 值。我们通过将字符指定为 int 来将给定字符的值存储在形式说明符中。因此,该字符的 ASCII 值存储在格式说明符中。
Java
// Java program to print ASCII Value of Character
// using format specifier
// Importing format library
import java.util.Formatter;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to compute
char character = '}';
// Initializing the format specifier
Formatter formatSpecifier = new Formatter();
// Converting the character to integer and
// ASCII value is stored in the format specifier
formatSpecifier.format("%d", (int)character);
// Print the corresponding ASCII value
System.out.println(
"The ASCII value of the character ' "
+ character + " ' is " + formatSpecifier);
}
}
The ASCII value of the character ' } ' is 125
方法 4:通过生成字节查找 ASCII 值(最优化)
- 将字符初始化为字符串。
- 使用 getBytes() 方法创建字节类型的数组。
- 在字节数组的第 '0' 个索引处打印元素。
这是位于字符串第 0 个索引处的字符的 ASCII 值。此方法通常用于将整个字符串转换为其 ASCII 值。对于违反编码异常的字符,给出try-catch。
Java
// Java program to print ASCII Value of Character
// by generating bytes.
// Importing I/O library
import java.io.UnsupportedEncodingException;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Try block to check exception
try {
// Character is initiated as a string
String sp = "}";
// An array of byte type is ccreated
// by using getBytes method
byte[] bytes = sp.getBytes("US-ASCII");
/*This is the ASCII value of the character
/ present at the '0'th index of above string.*/
// Printing the element at '0'th index
// of array(bytes) using charAt() method
System.out.println("The ASCII value of "
+ sp.charAt(0) + " is "
+ bytes[0]);
}
// Catch block to handle exception
catch (UnsupportedEncodingException e) {
// Message printed foe exception
System.out.println("OOPs!!!UnsupportedEncodingException occurs.");
}
}
}
The ASCII value of } is 125