实现代码如下:
public class CharCode {
public static void main(String[] args) {
String strChina = "中国";
for (int i = 0; i < strChina.length(); i++) {
System.out.println(Integer.toHexString((int) strChina.charAt(i)));// 打印Unicode编码
}
try {
byte[] buf = strChina.getBytes("gb2312");// 系统默认的本地字符编码是gb2312,所以可省略
for (int i = 0; i < buf.length; i++) {
System.out.println(Integer.toHexString(buf[i]));// 打印国标2312编码
}
for (int i = 0; i < buf.length; i++) {
System.out.write(buf[i]);
}
System.out.println();// 当碰到换行时会自动调用flush函数刷新缓冲区
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果: