问问题
643 次
1 回答
4
So the original string has first been encoded as utf8, then interpreted in iso-8859-1 and then the result again encoded as utf-8. Solution given in java. Assumes you have the raw byte access, otherwise more code is required to get them.
//The underlying bytes are these, based on the characters being displayed in windows-1251
byte[] rawBytes = {(byte)0xc3,(byte)0x90,(byte)0xc2,(byte)0x9f,(byte)0xc3,(byte)0x90,(byte)0xc2,
(byte)0xbe,(byte)0xc3,(byte)0x90,(byte)0xc2,(byte)0xbb,(byte)0xc3,(byte)0x90,
(byte)0xc2,(byte)0xbd,(byte)0xc3,(byte)0x91,(byte)0xc2,(byte)0x8b,(byte)0xc3,
(byte)0x90,(byte)0xc2,(byte)0xb9,(byte)0x20,(byte)0xc3,(byte)0x90,(byte)0xc2,
(byte)0xba,(byte)0xc3,(byte)0x90,(byte)0xc2,(byte)0xb0,(byte)0xc3,(byte)0x90,
(byte)0xc2,(byte)0xb4,(byte)0xc3,(byte)0x91,(byte)0xc2,(byte)0x80};
//alternatively this will work just as well:
//Charset windows1251 = Charset.forName("Windows-1251");
//byte[] rawBytes = windows1251.encode("Полный кадр").array();
Charset utf8 = Charset.forName("utf-8");
String asUTF8 = utf8.decode(ByteBuffer.wrap(rawBytes)).toString();
//Intermediate step required to convert the intermediate string
//to byte[] again. Iso-8859-1 is used because it maps 256 first
//unicode points exactly to byte values of 0-255
Charset iso88591 = Charset.forName( "ISO-8859-1");
byte[] bytes = iso88591.encode(asUTF8).array();
String finalResult = utf8.decode( ByteBuffer.wrap(bytes)).toString();
System.out.println(finalResult);
//Полный кадр
于 2012-12-07T15:32:22.440 回答