functiontoUnicodeFun(data){ if(data == '' || typeof data == 'undefined') return'请输入汉字'; var str =''; for(var i=0;i<data.length;i++){ str+="\\u"+data.charCodeAt(i).toString(16); } return str; }
var resultUnicode = toUnicodeFun('中国'); // \u4e2d\u56fd
Unicode转化为汉字
1 2 3 4 5 6 7 8 9 10 11
functiontoChineseWords(data){ if(data == '' || typeof data == 'undefined') return'请输入十六进制unicode'; data = data.split("\\u"); var str =''; for(var i=0;i<data.length;i++){ str+=String.fromCharCode(parseInt(data[i],16).toString(10)); } return str; } var resultChineseWords = toChineseWords("\u4e2d\u56fd"); console.log(resultChineseWords);//中国
1 2 3 4 5 6 7 8 9 10
var GB2312UnicodeConverter={ ToUnicode:function(str){ returnescape(str).toLocaleLowerCase().replace(/%u/gi,'\\u'); }, ToGB2312:function(str){ returnunescape(str.replace(/\\u/gi,'%u')); } }; var result = GB2312UnicodeConverter.ToUnicode('中国'); //\u4e2d\u56fd var result2 = GB2312UnicodeConverter.ToUnicode(result); //%5cu4e2d%5cu56fd