JS中文字符串转换unicode编码函数

当我们需要用href 传递中文参数时 我们可以用js的unicode函数对汉字进行转码 如我们使用 百度的时候汉字条件就会被编码

http://www.baidu.com/s?wd=%BA%C3%B5%C4%BA%DC

AJAX使用GET请求时传递中文字符串时也必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数如下

  1. function uniencode(text)
  2. {
  3. text = escape(text.toString()).replace(/\+/g, "%2B");
  4. var matches = text.match(/(%([0-9A-F]{2}))/gi);
  5. if (matches)
  6. {
  7. for (var matchid = 0; matchid < matches.length; matchid++)
  8. {
  9. var code = matches[matchid].substring(1,3);
  10. if (parseInt(code, 16) >= 128)
  11. {
  12. text = text.replace(matches[matchid], '%u00' + code);
  13. }
  14. }
  15. }
  16. text = text.replace('%25', '%u0025');
  17. return text;
  18. }

当然 如果你需要在服务器端得到utf-8码可以使用如下函数

  1. function convert_int_to_utf8($intval)
  2. {
  3. $intval=intval($intval);
  4. switch ($intval)
  5. {
  6. // 1 byte, 7 bits
  7. case 0:
  8. return chr(0);
  9. case ($intval&0x7F):
  10. return chr($intval);
  11. // 2 bytes, 11 bits
  12. case ($intval&0x7FF):
  13. return chr(0xC0|(($intval>>6)&0x1F)).
  14. chr(0x80|($intval&0x3F));
  15. // 3 bytes, 16 bits
  16. case ($intval&0xFFFF):
  17. return chr(0xE0|(($intval>>12)&0x0F)).
  18. chr(0x80|(($intval>>6)&0x3F)).
  19. chr (0x80|($intval&0x3F));
  20. // 4 bytes, 21 bits
  21. case ($intval&0x1FFFFF):
  22. return chr(0xF0|($intval>>18)).
  23. chr(0x80|(($intval>>12)&0x3F)).
  24. chr(0x80|(($intval>>6)&0x3F)).
  25. chr(0x80|($intval&0x3F));
  26. }
  27. }
  28. 如果你使用的是如java等后台语言的话 转码将更加简单了!!! 

前几天,遇到一个问题,就是在浏览器地址栏传递中文时,出现乱码,考虑了一下,解决方式有很多,我还是采用了转换编码的方式,将中文转换为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<zhStr.length();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<hex.length;i++){ // 注意要从 1 开始,而不是从0开始。第一个是空。
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();
}
}

posted on 2011-04-10 11:39  jiezzy  阅读(17386)  评论(1)    收藏  举报