在 Android 的 textview 中显示 UTF16-LE 编码的字符串 [英] Showing a UTF16-LE encoded string in textview for Android

查看:34
本文介绍了在 Android 的 textview 中显示 UTF16-LE 编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自服务器的 UTF-16LE 编码字符串.我想在我的活动的 Textview 中打印该字符串.但是,字符串打印时中间有空格.因此,Hello"打印为H e l l o"并且在我的屏幕上看起来不太好.

I have a UTF-16LE encoded string that comes from a server. I would like to print that string in Textview of my activity. However, the string prints with spaces in between them. So, "Hello" prints as "H e l l o" and doesn't look all that nice in my screen.

感谢任何帮助.

谢谢

推荐答案

假设您有一个包含 UTF-16LE 编码字符串的流(或 byte 数组).

Assuming you have a stream (or array of bytes) containing a UTF-16LE encoded string.

String str0 = "Hello, I am a UTF-16LE encoded String";
byte[] utf16le = str.getBytes("UTF-16LE");

如果您不将这些转换回 &说明使用的字符集,您将在结果字符串中生成一个包含大量 0 字节(UTF-16LE 显然是 16 位)的字符串.

If you do not convert these back & stating the character set used you will be producing a string containing a lot of 0-bytes (UTF-16LE is, obviously, 16-bit) in your resulting String.

String wrong = new String(utf16le); // This will produce crap with \0:s in it.
String correct = new String(utf16le, "UTF-16LE"); // This will be the actual string.

注意:如果你把像这样的垃圾字符串:s 转储到 ICS 中的 TextView 中,它会为你清除垃圾,而不是打印H e l l o".

Note: If you dump crap String:s like these into a TextView in ICS it will remove the garbage for you and not print "H e l l o".

这篇关于在 Android 的 textview 中显示 UTF16-LE 编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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