- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 Android 应用程序检索 SHOUTcast 元数据并显示它。我遇到了非英语字符集问题。基本上,元数据显示为乱码。我将如何执行字符编码检测并正确显示文本?抱歉,如果这是一个不平凡的问题,我对这个话题不是很精通。
有问题的流是:http://skully.hopto.org:8000
最佳答案
正如 vorrtex 在他上面的评论中指出的那样,如果您的数据以格式良好的 HTML 代码形式出现,您可以从 <meta content="...">
知道它的编码。标签,这是最好的场景。您可以使用如下代码将其转换为 Android(或其他 Java 实现)字符串:
// assume you have your input data as byte array buf, and encoding
// something like "windows-1252", "UTF-8" or whatever
String str = new String(buf, encoding);
// now your string will display correctly
如果您不知道编码 - 您收到的数据是未知格式的原始文本 - 您仍然可以使用统计语言模型尝试算法来猜测它。我刚刚在 http://site.icu-project.org/ 找到了 IBM 的 ICU - Unicode 国际组件 项目,具有自由开源许可(商业用途 OK)
它们同时提供 Java 和 C++ 库。我刚刚添加了他们的 Java JAR 版本。 51.2 到我的 Android 项目,它工作得很好。我用来从文本文件中识别字符编码的代码是:
public static String readFileAsStringGuessEncoding(String filePath)
{
String s = null;
try {
File file = new File(filePath);
byte [] fileData = new byte[(int)file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(fileData);
dis.close();
CharsetMatch match = new CharsetDetector().setText(fileData).detect();
if (match != null) try {
Lt.d("For file: " + filePath + " guessed enc: " + match.getName() + " conf: " + match.getConfidence());
s = new String(fileData, match.getName());
} catch (UnsupportedEncodingException ue) {
s = null;
}
if (s == null)
s = new String(fileData);
} catch (Exception e) {
Lt.e("Exception in readFileAsStringGuessEncoding(): " + e);
e.printStackTrace();
}
return s;
}
上面的
Lt.d 和 Lt.e 只是我对 Log.d(TAG, "blah...") 的快捷方式.在我能想到的所有测试文件上都运行良好。我有点只关心 APK 文件大小——icu4j-51_2.jar 超过 9 MB,而我的整个包在添加它之前只有 2.5 MB。但是隔离 CharsetDetector 及其依赖项很容易,所以我最后添加的大小不超过 50 kB。我需要从 ICU 源代码复制到我的项目的 Java 类都在 core/src/com/ibm/icu/text 目录下,它们是:
CharsetDetector
CharsetMatch
CharsetRecog_2022
CharsetRecog_mbcs
CharsetRecog_sbcs
CharsetRecog_Unicode
CharsetRecog_UTF8
CharsetRecognizer
此外,在 CharsetRecog_sbcs.java 中还有一个 protected “ArabicShaping as;”成员,它想要拉出更多的类,但事实证明对于字符集识别来说不需要它,所以我把它注释掉了。就这样。希望对您有所帮助。
格雷格
关于android - Android 中的字符集检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445820/
我正在使用使用jetty的Spring-Web应用程序: org.springframework.boot spring-boot-starter-web
我想匹配空格字符( )仅当它们后跟一个哈希( # )。 这是什么( #)下面是尝试做的,这是一个捕获组。 (我尝试转义括号,否则无法在组集中正确识别括号)。但是,这是行不通的。 下面的正则表达式 /#
我正在实现一个词法扫描器。我如何实现字符集?大多数字符集都采用范围形式,即 A-Z、h-L 等。我必须检查字符 ch 是否是字符集的成员。除了数组和位集之外,我可以在 Java 中使用哪种高效的数据结
我今天注意到我们的数据库使用字符集“utf8 -- UTF-8 Unicode”和排序规则“utf8_general_ci”,但里面的大多数表和列都使用 CHARSET=latin1。我会遇到任何问题
我有一个包含多种语言的 Mysql 表,一种语言一个字段。 我的字符集是utf_general_ci 当我用 phpMyAdmin 查看表格时,我有一个保加利亚语页面,如下所示: За наÑ
根据 MSDN vb.net uses this extended character set .根据我的经验,它实际上使用了这个: 我错过了什么?为什么它说它使用一个而使用另一个? 难道我做错了什么
我在 Symfony2 中有一个项目,它在我的本地主机上运行良好,但是在将其移动到外部服务器后问题已经开始。 我没有从包含波兰语字符的数据库中看到任何结果名称 在 Profiler 中,我检查了查询:
我更新了我的网络应用程序以使用 UTF-8 而不是 ANSI。 我做了以下措施来定义字符集: mysql_set_charset("utf8"); // PHP // HTML utf8_gener
typedef unsigned char Set; Set s1,s2; s1 = 0xda; PRINT(s1); printf("%d\n", s1); s2 = -s1; pri
我有一个 PHP/MySQL 应用程序,它需要在幕后处理 UTF-8 字符(UTF-8 字符不会显示在屏幕上)。 UTF-8 字符来自 PHP cURL 请求。 我需要做什么才能使 PHP 和 MyS
我正在使用 utf8_general_ci 作为字符集在 MySQL 中构建一个用户表。 1-) 使用这个字符集,两个用户一个叫 Bob 另一个叫 bob 看起来是同一个,对吧?我不知道这可能会导致一
我知道之前已经回答过这个问题,但我发现的解决方案不适用于我的系统(我已经测试过了)。 我想更改 Mysql 中的默认排序规则。这似乎设置为 latin1_swedish_ci,我想将其更改为 UTF8
我正在开发一个母语学习应用程序。我需要将一些字符存储为“ẽũ”。我的数据库设置为具有默认排序规则的 utf-8 字符集,以及受此字符影响的表。 问题是当我尝试使用常规 SQL 插入添加一行时: INS
有什么方法可以定义字符集,以及属于 的其他信息吗? jsFiddle 上的标签? 我知道他们侧面板的“信息”部分,但不允许 标签。 提前致谢! 最佳答案 是的,把它放在CSS部分: 使用 jQ
我正在创建一个对象运行时: var myObj = {}; myObj[propertyName] = propertyValue; propertyName 是变量,如果它像“a.b”,我的对象就有
假设我有一个值为 42 (*) 的字符。我需要在 n 行中打印这个字符,n 由用户定义。但是,对于每个换行,都必须打印另一个 *。如果用户输入“6”,那么结果将是这样的: * ** *** ****
该标准定义了 基本源字符集 基本执行字符集及其对应的宽字符 它还定义了“执行字符集”及其对应的宽字符,如下所示 $2.2/3- "The execution character set and the
我想 ping 目标 IP 地址并接收响应。为此,我在 Java 中使用带有 runtime.exec 方法和进程类的 Windows 命令行。我正在使用 inputStreamReader 获取响应
是否有任何方法可以更改 MySQL Workbench 字符集?我的架构使用 UTF-8,当我查看表数据(另存为 UTF-8)或手动添加数据时,出现字符集错误,可能 MySQL Workbench 使
我有一个文本选择/规范对话框,用户可以从列表中指定字体,设置粗体、斜体等。然后我使用 LOGFONT 中的信息创建一个 CFont 随后在 CEdit 中用作预览。 但是,如果用户选择像 Wingdi
我是一名优秀的程序员,十分优秀!