java转码问题

[复制链接]
查看11 | 回复4 | 2009-6-16 18:21:38 | 显示全部楼层 |阅读模式
public String convertString(String gbk){
String utf8 = "";
try {
utf8 = new String(gbk2utf8(gbk),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return utf8;
}
public byte[] gbk2utf8(String chenese) {
char c[] = chenese.toCharArray();
byte[] fullByte = new byte[3 * c.length];
for (int i = 0; i < c.length; i++) {
int m = (int) c;
String word = Integer.toBinaryString(m);
StringBuffer sb = new StringBuffer();
int len = 16 - word.length();
for (int j = 0; j < len; j++) {
sb.append("0");
}
sb.append(word);
sb.insert(0, "1110");
sb.insert(8, "10");
sb.insert(16, "10");
String s1 = sb.substring(0, 8);
String s2 = sb.substring(8, 16);
String s3 = sb.substring(16);
byte b0 = Integer.valueOf(s1, 2).byteValue();
byte b1 = Integer.valueOf(s2, 2).byteValue();
byte b2 = Integer.valueOf(s3, 2).byteValue();
byte[] bf = new byte[3];
bf[0] = b0;
fullByte[i * 3] = bf[0];
bf[1] = b1;
fullByte[i * 3 + 1] = bf[1];
bf[2] = b2;
fullByte[i * 3 + 2] = bf[2];
}
return fullByte;
}调用第一个函数转换
回复

使用道具 举报

千问 | 2009-6-16 18:21:38 | 显示全部楼层
returnnew String(rt.getBytes(“ISO-8859-1”),"utf-8") ;应该能行
回复

使用道具 举报

千问 | 2009-6-16 18:21:38 | 显示全部楼层
转送服务器前转为UTF-8接收后再转回GBK
回复

使用道具 举报

千问 | 2009-6-16 18:21:38 | 显示全部楼层
get提交用new String(rt.getBytes(“ISO-8859-1”),"GBK")
回复

使用道具 举报

千问 | 2009-6-16 18:21:38 | 显示全部楼层
xxx.writeUTF("防止中文乱码")用ISO-8859-1 编码行不?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则