一、汉字转GB2312编码工具类
package com.zb.doSth;
import java.io.UnsupportedEncodingException;
public class ChineseToGB2312 {
public String go(String chinese) throws UnsupportedEncodingException {
// 先把字符串按gb2312转成byte数组
byte[] bytes = chinese.getBytes("gb2312");
StringBuilder gbString = new StringBuilder();
// 遍历数组
for (byte b : bytes){
// 再用Integer中的方法,把每个byte转换成16进制输出
String temp = Integer.toHexString(b);
// 截取
temp = temp.substring(6, 8);
gbString.append("%").append(temp);
}
return gbString.toString();
}
}