mysql unicode转换为中文_中文转换成Unicode编码 和 Unicode编码转换为中文

前几天,遇到一个问题,就是在浏览器地址栏传递中文时,出现乱码,考虑了一下,解决方式有很多,我还是采用了转换编码的方式,将中文转换为Unicode编码,然后再解码成中文,以下是实现的过程,非常简单!

package cy.code;

public class CyEncoder {

private String zhStr; //中文字符串

private String unicode;//将中文字符串转换为Unicode编码 存储在这个属性上。

public CyEncoder(String zhStr){

this.zhStr = zhStr;

}

public String getZhStr() {

return zhStr;

}

public void setZhStr(String zhStr) {

this.zhStr = zhStr;

}

public String toUnicode(){

StringBuffer unicode = new StringBuffer();

for(int i=0; i

char c = zhStr.charAt(i);

unicode.append("\\u" + Integer.toHexString(c));

}

this.unicode = unicode.toString();

return unicode.toString();

}

public String tozhCN(){

StringBuffer gbk = new StringBuffer();

String[] hex = unicode.split("\\\\u");  // 妈的,分割让我想了半天!!不是"\\u",而是 "\\\\u"

for(int i=1;i

int data = Integer.parseInt(hex[i],16);  //  将16进制数转换为 10进制的数据。

gbk.append((char)data);  //  强制转换为char类型就是我们的中文字符了。

}

System.out.println("这是从 Unicode编码 转换为 中文字符了: "  +gbk.toString());

return gbk.toString();

}

public static void main(String args[]){

CyEncoder fc = new CyEncoder("为布局发的说法");

System.out.println(fc.toUnicode());

fc.tozhCN();

}

}

(转自:http://w2c2y2.iteye.com/blog/468140)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值