java getbytes 乱码_深入解析java String中getBytes()的编码问题

Java服务器后台在和Android端App通信时,遇到了两端关于用MD5加密同一包含中文的字符串结果不一致的问题。

具体问题描述:

Java服务器后台和Android端AS用了同一个MD5的工具类,且两边项目的默认编码都是UTF-8 ,加密纯英文数字的字符串时,结果一致,对同一包含中文的字符串加密,发现结果不一样,这是为什么呢?

工具类MD5Util代码如下:

public classMD5Util {/*** 将byte数组转化为16进制输出

*@parambytes

*@return

*/

public static String convertByteToHexString(byte[] bytes){

String result="";for (int i = 0; i < bytes.length; i++) {int temp=bytes[i]&0xff;

String tempHex=Integer.toHexString(temp);if(tempHex.length()<2){

result+="0"+tempHex;

}else{

result+=tempHex;

}

}returnresult;

}/*** MD5加密

*@parammessage

*@return*@throwsUnsupportedEncodingException*/

public static String md5Jdk(String message) throwsUnsupportedEncodingException{

String temp="";try{

MessageDigest md5Digest=MessageDigest.getInstance("MD5");byte[] encodeMD5Digest=md5Digest.digest(message.getBytes());

temp=convertByteToHexString(encodeMD5Digest);

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();

}returntemp;

}

}

最后问题锁定在:

1618382a6ad1a863a043ce6871d2b934.png

d1e19c8c4f04f6163745145344f12afc.png

IDE的项目默认编码和平台运行环境的编码不是一回事。

改成:message.getBytes("UTF-8");后能解决中文加密不一致的问题。

8cd4fb0c329517fb9a98d66198cde6e8.png

下面,就String的getBytes()方法深入了解下。

String的getBytes()方法是得到一个字符串的字节数组,但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组。如果你在使用这个方法时不考虑这一点,你会发现在一个平台上运行良好的系统,放到另一台机器上会产生意想不到的问题。

比如下面的程序:

public classTestCharset {public static voidmain(String[] args) {newTestCharset().execute();

}private voidexecute() {

String s= "Hello!你好!";byte[] bytes =s.getBytes();

System.out.println("bytes lenght is:" +bytes.length);

}

}

在一个中文WindowsXP系统下,运行时,结果为:

bytes lenght is:12

但是如果放到了一个英文的UNIX环境下运行:

$ java TestCharset bytes lenght is:9

如果你的程序依赖于该结果,将在后续操作中引起问题。为什么在一个系统中结果为12,而在另外一个却变成了9?上面已经提到了,该方法是和平台(编码)相关的。

在中文操作系统中,getBytes方法返回的是一个GBK或GB2313的中文编码的字节数组,其中中文字符各占两个字节。而在英文平台中,一般的默认编码是“ISO-8859-1”,每个字符都只取一个字节(而不管是否非拉丁字符)。

Java中的编码支持

Java是支持多国编码的,在Java中,字符都是以Unicode进行存储的,比如,“你”字的Unicode编码是“4f60”,我们可以通过下面的实验代码来验证:

public classTestCharset {public static voidmain(String[] args) {char c = '你';int i =c;

System.out.println(c);

System.out.println(i);

}

}

不管你在任何平台上执行,都会有相同的输出:

20320

20320就是Unicode “4f60”的整数值。其实,你可以反编译上面的类,可以发现在生产的.calss文件中字符“你”(或者其它任何中文字串)本身就是以Unicode编码进行存储的:

char c = '/u4F60'; ... ...

即使你知道了编码的编码格式,比如:

javac -encoding GBK TestCharset.java

编译后生成的.class文件中仍然是以Unicode格式存储中文字符或字符串的。

所以。为了避免这种问题,建议大家都在编码中使用String.getBytes(String charset)方法。

下面我们将从字串分别提取ISO-8859-1和GBK两种编码格式的字节数组,看看会有什么结果:

public classTestCharset {public static voidmain(String[] args) {newTestCharset().execute();

}private voidexecute() {

String s= "Hello!你好!";byte[] bytesISO8859 = null;byte[] bytesGBK = null;try{

bytesISO8859= s.getBytes("iso-8859-1");

bytesGBK= s.getBytes("GBK");

}catch(java.io.UnsupportedEncodingException e) {

e.printStackTrace();

}

System.out.println("-------------- /n 8859 bytes:");

System.out.println("bytes is: " +arrayToString(bytesISO8859));

System.out.println("hex format is:" +encodeHex(bytesISO8859));

System.out.println();

System.out.println("-------------- /n GBK bytes:");

System.out.println("bytes is: " +arrayToString(bytesGBK));

System.out.println("hex format is:" +encodeHex(bytesGBK));

}public static final String encodeHex(byte[] bytes) {

StringBuffer buff= new StringBuffer(bytes.length * 2);

String b;for (int i = 0; i < bytes.length; i++) {

b=Integer.toHexString(bytes[i]);//byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节

buff.append(b.length() > 2 ? b.substring(6, 8) : b);

buff.append(" ");

}returnbuff.toString();

}public static final String arrayToString(byte[] bytes) {

StringBuffer buff= newStringBuffer();for (int i = 0; i < bytes.length; i++) {

buff.append(bytes[i]+ " ");

}returnbuff.toString();

}

}

执行结果:

-------------- /n 8859bytes:

bytes is:72 101 108 108 111 33 63 63 63hex format is:48 65 6c 6c 6f 213f 3f 3f-------------- /n GBK bytes:

bytes is:72 101 108 108 111 33 -60 -29 -70 -61 -93 -95hex format is:48 65 6c 6c 6f 21 c4 e3 ba c3 a3 a1

可见,在s中提取的8859-1格式的字节数组长度为9,中文字符都变成了“63”,ASCII码为63的是“?”,一些国外的程序在国内中文环境下运行时,经常会出现乱码,上面布满了“?”,就是因为编码没有进行正确处理的结果。

而提取的GBK编码的字节数组中正确得到了中文字符的GBK编码。字符“你”、“好”、“!”的GBK编码分别是:“c4e3”、“bac3”、“a3a1”。得到了正确的以GBK编码的字节数组,以后需要还原为中文字串时,可以使用下面方法:

new String(byte[] bytes, String charset)

如果此文对您有帮助,微信打赏我一下吧~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值