I have a String in UTF-8
which I first convert into ISO-8859_1
and then convert it back to UTF-8
and get ISO8859_1
bytes from it. The result is supposed to be ISO-8859-1
again, but instead it gives me UTF-8
bytes. Why?
import java.io.UnsupportedEncodingException;
public class Test {
public static void main(String[] args) throws
UnsupportedEncodingException {
String s0 = "H\u00ebllo";
byte[] bytes = s0.getBytes("ISO8859_1");
byte[] bytes1=s0.getBytes("UTF-8");
printBytes(bytes, "bytes"); //72 -21 108 108 111 (ISO-8859-1)
printBytes(bytes1, "bytes1"); //72 -61 -85 108 108 111 (UTF-8)
byte[] bytes2=new String(s0.getBytes("UTF-8"), "ISO8859_1").getBytes("ISO8859_1");
printBytes(bytes2, "bytes2"); //72 -61 -85 108 108 111 (UTF-8)
}
private static void printBytes(byte[] array, String name) {
System.out.print(name+": ");
for(int i=0; i<array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}