JS中汉字和UTF-8编码相互转化

参考:https://www.cnblogs.com/iwang5566/p/6798799.html

https://blog.csdn.net/weixin_30527143/article/details/99586265

http://blog.chinaunix.net/uid-11612688-id-67157.html

https://www.iteye.com/blog/coolpep-1344467

https://blog.csdn.net/xinghuo0007/article/details/53064038

 

// UTF8编码转成汉字字符串
function revertUTF8(szInput) {
	var x,wch,wch1,wch2,uch="",szRet="";
	for (x=0; x<szInput.length; x++) {
		if (szInput.charAt(x)=="%") {
			wch =parseInt(szInput.charAt(++x) + szInput.charAt(++x),16);
			if (!wch) {break;}
			if (!(wch & 0x80)) {
				wch = wch;
			} else if (!(wch & 0x20)) {
				x++;
				wch1 = parseInt(szInput.charAt(++x) + szInput.charAt(++x),16);
				wch  = (wch & 0x1F)<< 6;
				wch1 = wch1 & 0x3F;
				wch  = wch + wch1;
			} else {
				x++;
				wch1 = parseInt(szInput.charAt(++x) + szInput.charAt(++x),16);
				x++;
				wch2 = parseInt(szInput.charAt(++x) + szInput.charAt(++x),16);
				wch  = (wch & 0x0F)<< 12;
				wch1 = (wch1 & 0x3F)<< 6;
				wch2 = (wch2 & 0x3F);
				wch  = wch + wch1 + wch2;
			}
			szRet += String.fromCharCode(wch);
		} else {
			szRet += szInput.charAt(x);
		}
	}
	return(szRet);
}

 

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值