在Java

时间:2015-06-16 01:32:09

标签: java unicode

假设中文字符串中有一个字符串"你好",将字符串转换为Unicode的正确结果是" \ u4f60 \ u597d",我得到的结果是下面的代码是" \ ufffd \ ufffd \ ufffd"。为什么???

Java代码:

String str = "你好";
public String testEncoding(String str) {
    String result = "";
    for(char ch : str.toCharArray())
        result += "\\u" + Integer.toHexString(ch | 0x10000).substring(1);
    System.out.print(result);
    return result;
}

String text = testEncoding("你好");
public static void input(String text){
    execShellCmd(String.format("am broadcast -a ADB_INPUT_TEXT --es msg \'%s\'", text));
}

private static void execShellCmd(String cmd) {
    try {
        Process process = Runtime.getRuntime().exec("su");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.write(cmd.getBytes("UTF-8"));
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

如果我设置了text =" \ u4f60 \ u597d",终端显示"你好"。

2 个答案:

答案 0 :(得分:2)

源文件编码可能与编译器使用的编码不对应。根据您的源文件编码,使用javac -encoding utf-8javac -encoding gb2312或其他任何内容。

答案 1 :(得分:-2)

好吧,中文译成unicode或unicode译成中文,看到了link

有关Java演示,请参见:


    /**
        * 字符串转16进制内码
        * @param str ab一2?仯3?4
        * @return \\u61\\u62\\u4e00\\u32\\u2b802\\u4eef\\u33\\u2b82f\\u34\\u34
    */
    public static String stringToCodePoints(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        str.codePoints().forEach(cp -> stringBuilder.append("\\u").append(Integer.toHexString(cp)));
        return stringBuilder.toString();
    }

您可以在this

中看到更多内容
**CJK Unified Ideographs Extension B (U+20000 through U+2A6DD)**  
**CJK Unified Ideographs Extension C (U+2A700 through U+2B734)**  
**CJK Unified Ideographs Extension D (U+2B740 through U+2B81D)**  
**CJK Unified Ideographs Extension E (U+2B820 through U+2CEA1)**   
**CJK Unified Ideographs Extension F (U+2CEB0 through U+2EBE0)** 

在此范围内的代码也可以正常运行。