简体   繁体   English

默认编码的字节数组转为 ISO-8859-1 编码的字节数组

[英]byte array of default encoding to byte array of ISO-8859-1 encoding

i have a byte array having default encoding.我有一个具有默认编码的字节数组。
i want to change that byte array to byte array of "ISO-8859-1" encoding.我想将该字节数组更改为"ISO-8859-1"编码的字节数组。
How to do this..?这该怎么做..? Please help me请帮我

byte[] isoBytes = new String(curBytes).getBytes("ISO-8859-1");

但是请注意,如果默认编码已经“丢失”了一些字符,则无法通过这种方式恢复它们。

String str = new String(currentByteArray);
byte[] newByteArray = str.getBytes("ISO-8859-1");

OK, so you have a String transformed to a byte array using the default platform encoding (whatever it is. And you want to transform this string to a byte array using ISO-8859-1.好的,所以您使用默认平台编码(无论它是什么)将字符串转换为字节数组。并且您想使用 ISO-8859-1 将此字符串转换为字节数组。

The first step is thus to transform the byte array to a String:因此,第一步是将字节数组转换为字符串:

String s = new String(bytes); // default encoding used here

and then to transform it back to a byte array:然后将其转换回字节数组:

byte[] iso88591Bytes = s.getBytes("ISO-8859-1");

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

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