字符集转换Java [英] Charset conversion Java

查看:126
本文介绍了字符集转换Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好
我有一个场景,我需要转换默认字符集应该覆盖UTF-8。我使用下面的类。但我没有得到预期的输出。因为我使用一个默认UTF-8作为字符集的unix系统,我比较那里的结果。我在这个程序的某个地方出错了吗?

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();
  }
 }

}

打印输出

windows-1252
true
String*** ""

在字符串输出中的之后注意我有一个spl char,我没有在unix env

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.

不要忘记您在环境之间遇到的任何差异可能不是Java。如果你从控制台(我希望你是)调用你的Java程序,由控制台实际上将程序的输出转换为你在屏幕上看到的输出。因此,根据控制台 使用的字符集,完全有可能输出您期望的字符,但是控制台无法正确显示它们。

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.

这篇关于字符集转换Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆