Java代码中的UTF-8问题 [英] UTF-8 issue in Java code

查看:262
本文介绍了Java代码中的UTF-8问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个字符串'ÐалÐμнÐ,аÑÐ',而不是在Java代码中得到'Календари'。



我使用了

pre> String convert = new String(convert.getBytes(iso-8859-1),UTF-8)
String convert = new String(convert.getBytes ),UTF-8)


解决方案

代码是好的。看来你的问题是,你需要做一个特定的字符转换,也许你的真正的输入没有被正确编码。要测试,我将做一个标准的逐步CharSet编码/解码,以查看事情发生的地方。



您的编码看起来不错, http://docs.oracle.com/javase/1.6/docs/guide/intl/ encoding.doc.html



以下似乎正常运行:

  //我怀疑你的问题是在这里 - 确保你的字符串/ char流正确编码字符串。也就是说,确保您想要iso-8859-1作为输入字符。 

Charset charsetE = Charset.forName(iso-8859-1);
CharsetEncoder encoder = charsetE.newEncoder();

//我相信从这里到结束可能会保持不变,根据您张贴的示例。
Charset charsetD = Charset.forName(UTF-8);
CharsetDecoder decoder = charsetD.newDecoder();

ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inputString));
CharBuffer cbuf = decoder.decode(bbuf);
final String result = cbuf.toString();
System.out.println(result);


I'm getting a string 'ÐалендаÑÐ' instead of getting 'Календари' in Java code. How can I convert 'ÐалендаÑÐ' to 'Календари'?

I used

 String convert =new String(convert.getBytes("iso-8859-1"), "UTF-8") 
 String convert =new String(convert.getBytes(), "UTF-8") 

解决方案

I believe your code is okay. It appears that your problem is that you need to do a specific character conversion, and maybe your "real" input is not being encoded correctly. To test, I would do a standard step by step CharSet encoding/decoding, to see where things are breaking.

Your encodings look fine, http://docs.oracle.com/javase/1.6/docs/guide/intl/encoding.doc.html

And the following seems to run normally :

//i suspect your problem is here - make sure your encoding the string correctly from the byte/char stream. That is, make sure that you want "iso-8859-1" as your input characters. 

Charset charsetE = Charset.forName("iso-8859-1");
CharsetEncoder encoder = charsetE.newEncoder();

//i believe from here to the end will probably stay the same, as per your posted example.
Charset charsetD = Charset.forName("UTF-8");
CharsetDecoder decoder = charsetD.newDecoder();

ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inputString));
CharBuffer cbuf = decoder.decode(bbuf);
final String result = cbuf.toString();
System.out.println(result);

这篇关于Java代码中的UTF-8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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