我解析mp3标签。如何转换俄语西里尔字母的字符串?
String artist
- 我不知道什么是对编码
Ïåñíÿ ïðî íàäåæäó
- 在俄罗斯"Песня про надежду"
例如字符串我用http://code.google.com/p/juniversalchardet/
代码:
String GetEncoding(String text) throws IOException {
byte[] buf = new byte[4096];
InputStream fis = new ByteArrayInputStream(text.getBytes());
UniversalDetector detector = new UniversalDetector(null);
int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
String encoding = detector.getDetectedCharset();
detector.reset();
return encoding;
}
和隐蔽
new String(text.getBytes(encoding), "cp1251");
- 但这不行。
如果我使用UTF-16
new String(text.getBytes("UTF-16"), "cp1251")
回报 “юяПесняпронадежду” 空间 - 不为CHAR空间
编辑:
这个第一读字节
byte[] abyFrameData = new byte[iTagSize];
oID3DIS.readFully(abyFrameData);
ByteArrayInputStream oFrameBAIS = new ByteArrayInputStream(abyFrameData);
的String =新字符串(abyFrameData, “????”);
你是如何得到的字符串文本参数?或许这个问题与你如何创建探测器的输入有关。 java字符串总是UTF-16,所以这里已经有一些字符转换了。 – stevevls 2011-05-16 12:06:37
'new String(text.getBytes(“UTF-16”),“cp1251”)'不会做你认为它做的事。它实际上做的是取一个现有的字符串,检索它的字节为UTF-16,然后尝试通过假设这些字节字节是CP1251来创建一个新字符串。这是保证是错误的。 – Anon 2011-05-16 12:12:39
@ stevevls,嗯java字符串总是UTF-16,而不是Unicode http://download.oracle.com/javase/tutorial/i18n/text/index.html – mKorbel 2011-05-16 12:15:16