Why is conversion from UTF-8 to ISO-8859-1 not the same in Windows and Linux?

Posted by user1895307 on Stack Overflow See other posts from Stack Overflow or by user1895307
Published on 2012-12-11T16:58:03Z Indexed on 2012/12/11 17:03 UTC
Read the original article Hit count: 250

Filed under:
|
|

I have the following in code to convert from UTF-8 to ISO-8859-1 in a jar file and when I execute this jar in Windows I get one result and in CentOS I get another. Might anyone know why?

public static void main(String[] args) {

  try {

    String x = "Ä, ä, É, é, Ö, ö, Ü, ü, ß, «, »";

    Charset utf8charset = Charset.forName("UTF-8");
    Charset iso88591charset = Charset.forName("ISO-8859-1");

    ByteBuffer inputBuffer = ByteBuffer.wrap(x.getBytes());
    CharBuffer data = utf8charset.decode(inputBuffer);

    ByteBuffer outputBuffer = iso88591charset.encode(data);
    byte[] outputData = outputBuffer.array();

    String z = new String(outputData);

    System.out.println(z);
  }
  catch(Exception e) {
    System.out.println(e.getMessage());
  }
}

In Windows, java -jar test.jar > test.txt creates a file containing: Ä, ä, É, é, Ö, ö, Ü, ü, ß, «, »

but in CentOS I get: ??, ä, ??, é, ??, ö, ??, ü, ??, «, »

Help please!

© Stack Overflow or respective owner

Related posts about java

Related posts about utf-8