一、练习-数字对应的中文
找出 E5 B1 8C 这3个十六进制对应UTF-8编码的汉字
参考代码1
// 找出 E5 B1 8C 这3个十六进制对应UTF-8编码的汉字
byte[] bs = new byte[3];
bs[0] = (byte) 0xE5;
bs[1] = (byte) 0xB1;
bs[2] = (byte) 0x8C;
String str = new String(bs, "UTF-8");
System.out.println("E5B18C 对应的字符是:" + str);
参考代码2
String s = "E5B18C";
int total = s.length() / 2;
byte[] buffer = new byte[total];
for (int i = 0; i < total; i++) {
int start = i * 2;
//先将字符转换为16进制数字,再强制转换为字节
buffer[i] = (byte) Integer.parseInt(s.substring(start, start + 2), 16);
}
String result = new String(buffer, "UTF-8");
System.out.println(