编写JAVA程序输出中文字的unicode编码

编写java程序,输出的'一和'二'的unicode编码

public class Coder {

public static void main(String[] args) throws Exception {
String str = "无";
byte[] byteOfChar = str.getBytes("Unicode");
StringBuffer sb = new StringBuffer("\\u");
for (int i = 2; i < byteOfChar.length; i++) {
sb.append(getUnicode(byteOfChar[i]));
}
while(sb.length() < 6){
sb.append("0");
}
System.out.println(sb.toString());
}

// 用于获取一个字节的十六进制写法
private static String getUnicode(byte byteOfChar) {
int i = byteOfChar;
if(i < 0){
i = 128*2+i;
}
StringBuffer sb = new StringBuffer();
char[] unicode = null;
while (i != 0) {
switch (i % 16) {
case 10:
sb.append("A");
break;
case 11:
sb.append("B");
break;
case 12:
sb.append("C");
break;
case 13:
sb.append("D");
break;
case 14:
sb.append("E");
break;
case 15:
sb.append("F");
break;
default:
sb.append(i % 16);
break;
}

i = i / 16;
}
unicode = sb.toString().toCharArray();
sb = new StringBuffer();
for (int u = unicode.length - 1; u >= 0; u--) {
sb.append(unicode[u]);
}
return sb.toString();
}

}
将字符转换成unicode编码。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-21
我写的,你试试,你可以把它改写成循环的,可以一直把字符的Unicode输出,完善后发给我哈:
import java.io.*;
public class FindUnicode {
public static void main(String[] args) throws IOException{
InputStreamReader read = new InputStreamReader (System.in);
int ch = read.read();
System.out.print("\\u"+Integer.toHexString(ch));
read.close();
}
}
第2个回答  2013-08-21
比如这个程序输出三个Unicode字的
class Hello {
public static void main(String args[])
{
System.out.println("\u7234\u8011\u8756");
}
}本回答被网友采纳
第3个回答  2013-08-21
一 对应 \u4e00

二 对应 \u4e8c
相似回答