简体   繁体   English

如何在Android中将ASCII字符转换为字符串?

[英]How to convert ASCII characters to string in Android?

I am loading a text to TextView but the problem is that the text is full of ASCII characters, so when the text is loaded to the TextView, I can't see anything or it shows me completly other character. 我正在将文本加载到TextView,但是问题是文本充满了ASCII字符,因此当文本加载到TextView时,我什么也看不到,或者它完全显示了其他字符。 For example instead of character "ó" a "?" 例如,用字符“?”代替“?” within a black square is shown. 在黑色正方形内显示。

My question is, how can I convert an ASCII character to string? 我的问题是,如何将ASCII字符转换为字符串?

Thanks for helping. 感谢您的帮助。

€ is not an Ascii character. €不是Ascii字符。 Instead you probably are using Windows-1252 encoding on your source files, or similar. 相反,您可能在源文件或类似文件上使用Windows-1252编码。 There the € is mapped to a character 128, which in Unicode is a control character. 那里€映射到字符128,在Unicode中它是控制字符。

Edit: as it is apparent, that the text is loaded from the internet, and nonmodifiable, and whatnot, then the following code could work: 编辑:很明显,该文本是从Internet加载的,并且不可修改,是这样,那么以下代码可以工作:

InputStreamReader reader = new InputStreamReader(
        yourInputStream, "windows-1252"); 
        // or what ever seems to be the correct encoding!

StringBuilder builder = new StringBuilder();

while (reader.ready())
{
    builder.append(reader.read());
}
reader.close();

String string = builder.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM