求在java 中汉字转换成 ascii 的小程序

来源:百度知道 编辑:UC知道 时间:2024/06/14 08:34:22
速度 急用 速度够快的加分啊 有的是
不好意思 我不知道是不是变换为ascii来着
就是 jdk 中 用native2ascii 命令 转换汉字的 如:中 转换为 \u4e2d

import java.io.UnsupportedEncodingException;

public class Main {

public static byte[] hz2gbk(String str) {
try {
return str.getBytes("GBK");
} catch (UnsupportedEncodingException ex) {
return null;
}
}

public static void main(String[] args) {
String str = "你好";
byte[] b = hz2gbk(str);
System.out.print(str + "[");
for (int i = 0, max = b.length; i < max; i++) {
System.out.print(Integer.toHexString(b[i]&0xff));
if (i < max - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
}