我正在尝试将用 Java 编码的 UTF-8 字符串转换为 ISO-8859-1。例如,在字符串 'âabcd' 中,'â' 在 ISO-8859-1 中表示为 E2。在 UTF-8 中,它表示为两个字节。C3 A2 我相信。当我执行 getbytes(encoding) 然后使用 ISO-8859-1 编码中的字节创建一个新字符串时,我得到两个不同的字符。¢。有没有其他方法可以做到这一点,以保持字符相同,即âabcd?
8 回答
如果您正在处理 UTF-16 以外的字符编码,则不应该使用java.lang.String
或char
原语——您应该只使用byte[]
数组或ByteBuffer
对象。然后,您可以使用java.nio.charset.Charset
在编码之间进行转换:
Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-1");
ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[]{(byte)0xC3, (byte)0xA2});
// decode UTF-8
CharBuffer data = utf8charset.decode(inputBuffer);
// encode ISO-8559-1
ByteBuffer outputBuffer = iso88591charset.encode(data);
byte[] outputData = outputBuffer.array();
byte[] iso88591Data = theString.getBytes("ISO-8859-1");
会成功的。根据您的描述,您似乎正在尝试“存储 ISO-8859-1 字符串”。Java 中的字符串对象总是以 UTF-16 隐式编码。无法更改该编码。
你可以做的是获取构成它的其他编码的字节(使用.getBytes()
如上所示的方法)。
从一组使用 UTF-8 对字符串进行编码的字节开始,从该数据创建一个字符串,然后获取一些以不同编码对字符串进行编码的字节:
byte[] utf8bytes = { (byte)0xc3, (byte)0xa2, 0x61, 0x62, 0x63, 0x64 };
Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-1");
String string = new String ( utf8bytes, utf8charset );
System.out.println(string);
// "When I do a getbytes(encoding) and "
byte[] iso88591bytes = string.getBytes(iso88591charset);
for ( byte b : iso88591bytes )
System.out.printf("%02x ", b);
System.out.println();
// "then create a new string with the bytes in ISO-8859-1 encoding"
String string2 = new String ( iso88591bytes, iso88591charset );
// "I get a two different chars"
System.out.println(string2);
这会正确输出字符串和 iso88591 字节:
âabcd
e2 61 62 63 64
âabcd
所以你的字节数组没有与正确的编码配对:
String failString = new String ( utf8bytes, iso88591charset );
System.out.println(failString);
输出
âabcd
(或者,或者您只是将 utf8 字节写入文件并在其他地方以 iso88591 的形式读取它们)
这就是我需要的:
public static byte[] encode(byte[] arr, String fromCharsetName) {
return encode(arr, Charset.forName(fromCharsetName), Charset.forName("UTF-8"));
}
public static byte[] encode(byte[] arr, String fromCharsetName, String targetCharsetName) {
return encode(arr, Charset.forName(fromCharsetName), Charset.forName(targetCharsetName));
}
public static byte[] encode(byte[] arr, Charset sourceCharset, Charset targetCharset) {
ByteBuffer inputBuffer = ByteBuffer.wrap( arr );
CharBuffer data = sourceCharset.decode(inputBuffer);
ByteBuffer outputBuffer = targetCharset.encode(data);
byte[] outputData = outputBuffer.array();
return outputData;
}
对于文件编码...
public class FRomUtf8ToIso {
static File input = new File("C:/Users/admin/Desktop/pippo.txt");
static File output = new File("C:/Users/admin/Desktop/ciccio.txt");
public static void main(String[] args) throws IOException {
BufferedReader br = null;
FileWriter fileWriter = new FileWriter(output);
try {
String sCurrentLine;
br = new BufferedReader(new FileReader( input ));
int i= 0;
while ((sCurrentLine = br.readLine()) != null) {
byte[] isoB = encode( sCurrentLine.getBytes() );
fileWriter.write(new String(isoB, Charset.forName("ISO-8859-15") ) );
fileWriter.write("\n");
System.out.println( i++ );
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
static byte[] encode(byte[] arr){
Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-15");
ByteBuffer inputBuffer = ByteBuffer.wrap( arr );
// decode UTF-8
CharBuffer data = utf8charset.decode(inputBuffer);
// encode ISO-8559-1
ByteBuffer outputBuffer = iso88591charset.encode(data);
byte[] outputData = outputBuffer.array();
return outputData;
}
}
如果字符串中有正确的编码,则无需执行更多操作来获取其他编码的字节。
public static void main(String[] args) throws Exception {
printBytes("â");
System.out.println(
new String(new byte[] { (byte) 0xE2 }, "ISO-8859-1"));
System.out.println(
new String(new byte[] { (byte) 0xC3, (byte) 0xA2 }, "UTF-8"));
}
private static void printBytes(String str) {
System.out.println("Bytes in " + str + " with ISO-8859-1");
for (byte b : str.getBytes(StandardCharsets.ISO_8859_1)) {
System.out.printf("%3X", b);
}
System.out.println();
System.out.println("Bytes in " + str + " with UTF-8");
for (byte b : str.getBytes(StandardCharsets.UTF_8)) {
System.out.printf("%3X", b);
}
System.out.println();
}
输出:
Bytes in â with ISO-8859-1
E2
Bytes in â with UTF-8
C3 A2
â
â
除了亚当罗森菲尔德的回答,我想补充一点,它ByteBuffer.array()
返回缓冲区的底层字节数组,它不一定“修剪”到最后一个字符。将需要额外的操作,例如此答案中提到的操作;尤其是:
byte[] b = new byte[bb.remaining()]
bb.get(b);
驱逐非 ISO-8859-1 字符,将被替换为 '?' (例如发送到 ISO-8859-1 数据库之前):
utf8String = new String (utf8String.getBytes(), "ISO-8859-1" );