上代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入汉字:");
String input = sc.nextLine();
if (input.length() == 0) {
System.out.println("输入错误");
return;
}
System.out.println("汉字转unicode结果:");
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length; i++) {
String unicode = Integer.toHexString(chars[i]);
if (unicode.length() <= 2) {
// 不足四位前面加0补齐
unicode = "00" + unicode;
}
unicode = "\\u" + unicode;
System.out.println(chars[i] + ": " + unicode);
}
}
}