javanerd
javanerd

Reputation: 2852

Charset conversion Java

Hi I have a scenario where I need to convert the default Charset should be overridden bu UTF-8. I am using below class. But I am not getting the expected output. Because I use a unix system that has default UTF-8 as charset and I compare the results there. Am I wrong somewhere in this program?

public class CharsetDisplay {

 public static void main(String[] args) {
  System.out.println(Charset.defaultCharset().name());
  System.out.println(Charset.isSupported("UTF-8"));
  final Charset UTF8_CHARSET = Charset.forName("UTF-8");
  try {
   byte[] byteArray = new byte[] {34,34,0};
   String str = new String(byteArray,UTF8_CHARSET);
   System.out.println("String*** "+str);
   System.out.println("String to Hex *** "+stringToHex(str));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

Prints output as

windows-1252
true
String*** ""

Note after "" in the string output I have a spl char, which I don't get in a unix env

Upvotes: 0

Views: 1315

Answers (2)

bobince
bobince

Reputation: 536329

If Java doesn't pick up your locale's encoding properly you may have to tell it explicitly, at the command-line:

java -Dfile.encoding=utf-8 CharsetDisplay

Upvotes: 2

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

What do you expect the zero byte to render as in this environment? Your output looks exactly correct to me.

Don't forget that any differences that you encounter between environments might not be down to Java. If you're invoking your Java program from a console (which I expect you are), it's up to the console to actually convert the program's output to what you see on the screen. So depending on the charset the console is using, it's entirely possible for Java to output the characters that you expect, but for the console to fail to render them properly.

Upvotes: 2

Related Questions