[转载]经典java转码程序,实现native2ascii命令行的功能

public static String convert(String str)
{
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for(i = 0;i<str.length();i++){
c = str.charAt(i);
if (c > 255) {
sb.append("\\u");
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1) sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1) sb.append("0");
sb.append(tmp);
}
else {
sb.append(c);
}

}return(new String(sb));
}

posted on 2010-09-15 17:30  TankMa  阅读(724)  评论(0编辑  收藏  举报