java 中 byte[]-->String-->byte[] 怎么转? new String(buffin,0,len,"gbk").getBytes("gb2312") != buffinbuffin 类型byte[]String --> byte[]-->String 怎么转? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java技术交流,欢迎加入40652189,深入学习java的qq群,探讨新技术 我用的就是相同的字符集public String StringDecrypt(String strSource) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(strSource.getBytes("GBK")); byte[] buff = out.toByteArray(); out.close(); for(int i=0;i< buff.length;i++) { buff[i] = DecyptWithGroup(buff[i]); } ByteArrayInputStream in = new ByteArrayInputStream(buff); int len = in.available(); byte[] buffin = new byte[len]; in.read(buffin); in.close(); return new String(buffin,0,len,"GBK");} 最后返回的字符串new String(buffin,0,len,"GBK")而 new String(buffin,0,len,"GBK").getBytes("GBK") 得到的byte[] 中最后一个byte 和buffin中 最后一个不同我觉得太奇怪了! 我甚至想是不是new String(buffin,0,len,"GBK")得到的字符串添加东西了! Arrays.equals(byte[] a,byte[] a2) 各位大哥:小弟再把问题明了一下: 因为我做的是加解密,加密结果都是乱吗!所以问的问题有些不太确切!误解 这是我watch 出来的结果! "buffin"= byte[8] (id=21) [0]= 44 [1]= -84 [2]= -17 [3]= 19 [4]= -56 [5]= -10 [6]= 45 [7]= -35"new String(buffin,0,len,"GBK").getBytes("GBK")"= byte[8] (id=78) [0]= 44 [1]= -84 [2]= -17 [3]= 19 [4]= -56 [5]= -10 [6]= 45 [7]= 63 "new String(buffin,0,len,"GBK")"= ",撒-�" count= 6 hash= 0 offset= 0 value= char[6] (id=92) [0]= , [1]= [2]= [3]= 撒 [4]= - [5]= � 到底怎么作才能使转化正确呢??到底怎么作才能使转化正确呢?? //string 转 byte[]String str = "中文字符";byte[] strbyte = str.getBytes();// byte[] 转 stringString res = new String(strbyte);System.out.println(res);不要用String.valueof(strbyte). 时间格式 算法:寻找没有放入到数组中的数 请教一个版本控制问题 JavaScript正则表达式的一个问题 Hibernate+spring 连接多个数据库问题!在线急等!!!!!!!!!!!!!!!!!!!!!!!!!!!!! readUTF出错了。。。。。 -Xlint:unchecked怎么用 请问构造函数的的调用顺序? jbuild4安装问题 哪位知道哪里错了 可否把一个.java文件编译成exe,并且能完全脱离JVM执行,也就是这个exe可以放到没有JRE的机器上执行? 最牛的 JTable 的问题,200分相送,出血本了,从没有这么放过血(不够分在给)
public String StringDecrypt(String strSource) throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(strSource.getBytes("GBK"));
byte[] buff = out.toByteArray();
out.close();
for(int i=0;i< buff.length;i++)
{
buff[i] = DecyptWithGroup(buff[i]);
}
ByteArrayInputStream in = new ByteArrayInputStream(buff);
int len = in.available();
byte[] buffin = new byte[len];
in.read(buffin);
in.close();
return new String(buffin,0,len,"GBK");
}
最后返回的字符串new String(buffin,0,len,"GBK")
而 new String(buffin,0,len,"GBK").getBytes("GBK") 得到的byte[] 中最后一个byte 和buffin中 最后一个不同
我觉得太奇怪了! 我甚至想是不是new String(buffin,0,len,"GBK")得到的字符串添加东西了!
"buffin"= byte[8] (id=21)
[0]= 44
[1]= -84
[2]= -17
[3]= 19
[4]= -56
[5]= -10
[6]= 45
[7]= -35"new String(buffin,0,len,"GBK").getBytes("GBK")"= byte[8] (id=78)
[0]= 44
[1]= -84
[2]= -17
[3]= 19
[4]= -56
[5]= -10
[6]= 45
[7]= 63
"new String(buffin,0,len,"GBK")"= ",撒-�"
count= 6
hash= 0
offset= 0
value= char[6] (id=92)
[0]= ,
[1]=
[2]=
[3]= 撒
[4]= -
[5]= �
到底怎么作才能使转化正确呢??
到底怎么作才能使转化正确呢??
String str = "中文字符";
byte[] strbyte = str.getBytes();// byte[] 转 string
String res = new String(strbyte);
System.out.println(res);不要用String.valueof(strbyte).