java中字符编码问题

SunCd3210oqq 2010-05-05 04:03:10
给个例子:
1.String name = "中国";

2.String afterE=new String(name.getBytes(),"utf-8");

3.String then = new String(afterE.getBytes("utf-8"),"gbk");

4.System.out.println(then);

经过调试,发现name经过第二句话后变成了三个字符,当然是乱码,经过第四句后then为四个汉字,不是中国,为什么?
...全文
282 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
SunCd3210oqq 2010-05-08
  • 打赏
  • 举报
回复
我的file.encoding得出来的是GBK 编码,我想问一下,这个编码和JSP文件中的pageEncoding即:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
指定的编码有什么区别与关系,这句话不是指定编码方式吗?那我的字符串变量"中国"到底是以什么编码方式存在的呢,是系统默认的GBK还是指定的UTF-8?我发现我越来越搞不清楚了!
  • 打赏
  • 举报
回复
file.encoding 属性会根据运行环境的变化而变化。

因此,编码过程中一定不要使用默认的。
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 suncd3210oqq 的回复:]
括号里没有写具体的编码方式,那么它默认的是什么呢?
[/Quote]

默认采用 System.getProperty("file.encoding") 系统属性中的编码。
humanity 2010-05-07
  • 打赏
  • 举报
回复
在学习中,应该是不能直接变换的,如果楼主想知道如何翻译一个在UTF-8表示的“中国”成为一个用GBK表示的“中国”,似乎 Java 没有直接提供这功能,我们一般都是在处理 char->byte 后再正确还原 byte->char 的过程,不知道有没有现成的 API 可以提供字符集翻译的,我觉得需要一个对照表才行,因为电脑似乎只知道
e4 b8 ad e5 9b bd 而并不知道它就是"中国",如果让程序以”中国"为参照物,那就能直接得出它在不同字符集中的编码了。
SunCd3210oqq 2010-05-07
  • 打赏
  • 举报
回复
平台的默认编码方式是跟系统有关的吗?还是跟myeclipse的设置有关?
秋9 2010-05-06
  • 打赏
  • 举报
回复
编码必须一一对应,有GBK->UTF-8,就不需有UTF-8->GBK
chenhuaike 2010-05-06
  • 打赏
  • 举报
回复
转化顺序要配对,比如string由utf8、gbk、big5转化而来,则需要big5、gbk、utf8转化回去,也就是通常讲的从哪来回哪去,不过这个通常需要它转化的顺序,还不如直接使用setCharacterEncoding来 的直接
fyjava1984 2010-05-06
  • 打赏
  • 举报
回复
在java中,汉字是以Unicode码存在的,

String name = "中国";
String afterE = new String(name.getBytes("utf-8"),"gbk");
System.out.println(afterE);
String then = new String(afterE.getBytes("gbk"),"utf-8");
System.out.println(then);
若鱼1919 2010-05-06
  • 打赏
  • 举报
回复
String name = "中国";此时的“中国”在内存里面是用unicode编码的字符。
new String(name.getBytes(),"utf-8");
name.getBytes():把unicode编码的字符用gbk方式转化成字节数组,很显然,转化是行不通的!
然后,又把转化错了的字节数组用utf-8转化成字符,输入就错了,输出肯定不对!
ZHXDD 2010-05-06
  • 打赏
  • 举报
回复
String name = "中国";
2.String afterE=new String(name.getBytes("utf-8"),"utf-8");
3.String then = new String(afterE.getBytes("gbk"),"gbk");
4.System.out.println(then);
这样是对的,因为utf-8中char是2个字节,gbk中char是1个字节.
neozhengx 2010-05-06
  • 打赏
  • 举报
回复
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
和你的开发平台的默认编码格式相同
cwjieNo1 2010-05-06
  • 打赏
  • 举报
回复
字符编码方式的转化,不懂,帮顶·
SunCd3210oqq 2010-05-06
  • 打赏
  • 举报
回复
先谢谢大家.

刚才又试了一下这两句话:

System.out.println(new String(name.getBytes(),"gbk");

System.out.println(new String(name.getBytes("gbk"),"gbk");

发现都可以正常输出“中国”两个字,第二句似乎可以理解,但是第一句为什么也可以,第一句不应该等同于:System.out.println(new String(name.getBytes("utf-8"),"gbk");吗?

括号里没有写具体的编码方式,那么它默认的是什么呢?
hollowinheart 2010-05-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhxdd 的回复:]
String name = "中国";
2.String afterE=new String(name.getBytes("utf-8"),"utf-8");
3.String then = new String(afterE.getBytes("gbk"),"gbk");
4.System.out.println(then);
这样是对的,因为utf-8中char是2个字节,gbk中c……
[/Quote]

正解,要用什么编码显示就用什么编码取字节数组getBytes
ddyouyue 2010-05-06
  • 打赏
  • 举报
回复
then = new String(afterE.getBytes("utf-8"),"gbk");
你获得的是utf-8编码的字节,却传进gbk当然会不对了,编码他会根据你给的字节在编码表里面查找相应的字符,找不到或找错就是乱码或者奇怪文字
new String(byte[],Srting) 是将字节数组的字节转化为String(Unicode编码的),你要告诉他你给的是什么编码的字节数组
  • 打赏
  • 举报
回复
中国 UTF-8 编码为:

e4 b8 ad e5 9b bd

根据 GBK 的编码规范,重新组装成:

E4B8 ADE5 9BBD

E4B8 的 GBK 字为“涓”
ADE5 的 GBK 字为未被编码
9BBD 的 GBK 字为“浗”
SunCd3210oqq 2010-05-06
  • 打赏
  • 举报
回复
想问一下,为什么“中国”两字经过new String(getBytes("utf_8"),"gbk"),就变成了三个字符,它们之间是怎么对应的呢?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧