【问题标题】:I have converted stringbuffer to strings but how to convert string to unicode int?我已将 stringbuffer 转换为字符串,但如何将字符串转换为 unicode int?
【发布时间】:2018-05-23 17:50:42
【问题描述】:

如果我直接输入 unicode 并使用以下方法将其转换为 String,我可以看到表情符号:

int intUnicode = 0x1F601;

public String getEmojiByUnicode(int unicode) {
    return new String(Character.toChars(unicode));
}

但是当 unicode 值是 String 时,我无法理解如何转换:

String strUnicode = "0xf601";

如何将其转换为Integer,就像我直接做的那样。当我尝试使用Integer.parseInt(strUnicode) 时,编译器每次都会抛出错误。

InputStream is = assetManager.open("emoji_unicode.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is != null){
    try {
        while ((data = reader.readLine()) != null){
            sbuffer.append(data + "\n");
            String j = sbuffer.toString().trim();
            int k = Integer.parseInt(j,16);
            String l = getEmojiByUnicode(k);
            emoArray.add(l);
        }
        btn.setText(emoArray.get(5));
        is.close();
    }catch (IOException e){
        e.printStackTrace();
    }
}

这不是很难,我知道我很接近,但它仍然不会影响到我。任何帮助将不胜感激。 关注

【问题讨论】:

标签: java android string unicode int


【解决方案1】:

您正在尝试解析 hex 字符串(unicode“编码”是十六进制数字),Integer.parseInt() 这样做有一些奇怪的限制。看看这个答案:

Parsing a Hexadecimal String to an Integer throws a NumberFormatException?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 2011-10-27
    • 1970-01-01
    • 2014-10-21
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多