如何在Android中将UniCode字符串转换为文本 [英] How to convert UniCode String into Text in android

查看:1074
本文介绍了如何在Android中将UniCode字符串转换为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从如下服务器收到了json

I have received a json from server like below

{
    "AuthenticationMessage": "\\u0986\\u09aa\\u09a8\\u09bf \\u09ad\\u09c1\\u09b2 \\u09aa\\u09be\\u09b8\\u0993\\u09df\\u09be\\u09b0\\u09cd\\u09a1 \\u09a6\\u09bf\\u09df\\u09c7\\u099b\\u09c7\\u09a8"
}

我也有如下的Textview

and i also have a Textview like below

ValidationMessage = (TextView) findViewById(R.id.tv_validation_message);

现在我想在文本视图中打印"AuthenticationMessage"的值. 我已经完成了

now i want to print the value of "AuthenticationMessage" in the textview. i have done following

ValidationMessage.setText(response.getString("AuthenticationMessage"));

但它显示如下文本

\u0986\u09aa\u09a8\u09bf \u09ad\u09c1\u09b2 \u09aa\u09be\u09b8\u0993\u09df\u09be\u09b0\u09cd\u09a1 \u09a6\u09bf\u09df\u09c7\u099b\u09c7\u09a8

我正在寻找

আপনি ভুল পাসওয়ার্ড দিয়েছেন

我看错了什么.请引导我.

what i am looking wrong. please guide me.

推荐答案

我只是复制其他人的代码.

I just copy other's codes.

尝试搜索什么是unicode,以及如何将char转换为unicode.

Try to search what's unicode and how to convert char to unicode.

private static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }

这篇关于如何在Android中将UniCode字符串转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆