开发者

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 U开发者_如何学运维TF-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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜