js 中文汉字转Unicode、Unicode转中文汉字、ASCII转换Unicode、Unicode转换ASCII、中文转换函数代码
最近看不少在线工具里面都有一些编码转换的代码,很多情况下我们都用得到,这里毛票票小编就跟大家分享一下这些资料
Unicode介绍
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。
Unicode是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。
Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案。Unicode用数字0-0x10FFFF来映射这些字符,最多可以容纳1114112个字符,或者说有1114112个码位。码位就是可以分配给字符的数字。
Unicode到目前为止所定义的五个平面中,第0平面(BMP)最为重要,其编码中文汉字范围为:4E00-9FBFCJK统一表意符号(CJKUnifiedIdeographs)
ASCII介绍
ASCII是基于拉丁字母的一套电脑编码系统。它主要用于显示现代英语和其他西欧语言。
它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC646。
0-127是7位ASCII码的范围,是国际标准。至于汉字,不同的字符集用的ascii码的范围也不一样,常用的汉字字符集有GB2312-80,GBK,Big5,unicode等。
GB_2312字符集是目前最常用的汉字编码标准。在这个标准中,每个汉字用2个字节来表示,每个字节的ascii码为161-254(16进制A1-FE),第一个字节对应于区码的1-94区,第二个字节对应于位码的1-94位。
ASCII介绍
native2ascii是sunjavasdk提供的一个工具。用来将别的文本类文件(比如*.txt,*.ini,*.properties,*.java等等)编码转为Unicode编码。为什么要进行转码,原因在于程序的国际化。
安装了jdk后,假如你是在windows上安装,那么在jdk的安装目录下,会有一个bin目录,其中native2ascii.exe正是native2ascii中文转unicode工具。
native2ascii的命令行的命名格式:native2ascii-[options][inputfile[outputfile]]。
例如:native2asciizh.txtu.txt:将zh.txt转换为Unicode编码,输出文件到u.txt。
本工具中汉字与Unicode转换采用PHP开发,支持十六进制和十进制表示,能够中文汉字和Unicode互转;默认情况下采用十六进制。
下面函数都需要用到的函数
functionleft_zero_4(str){ if(str!=null&&str!=''&&str!='undefined'){ if(str.length==2){ return'00'+str; } } returnstr; }
中文汉字转Unicode
functionunicode(str){ varvalue=''; for(vari=0;i<str.length;i++){ value+='\\u'+left_zero_4(parseInt(str.charCodeAt(i)).toString(16)); } returnvalue; } functionleft_zero_4(str){ if(str!=null&&str!=''&&str!='undefined'){ if(str.length==2){ return'00'+str; } } returnstr; }
Unicode转中文汉字、ASCII转换Unicode
functionreconvert(str){ str=str.replace(/(\\u)(\w{1,4})/gi,function($0){ return(String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16))); }); str=str.replace(/(&#x)(\w{1,4});/gi,function($0){ returnString.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16)); }); str=str.replace(/(&#)(\d{1,6});/gi,function($0){ returnString.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2"))); }); returnstr; }
Unicode转换ASCII
functionunicode1(str){ varvalue=''; for(vari=0;i<str.length;i++) value+='&#'+str.charCodeAt(i)+';'; returnvalue; }
中文转换&
functionascii(str){ varvalue=''; for(vari=0;i<str.length;i++){ value+='\&#x'+left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';'; } returnvalue; }
完整的可以测试的代码
<scripttype="text/javascript"> functiona(pChoice){ varinputEle=document.getElementById('input_area'); varoutputEle=document.getElementById('output_area'); switch(pChoice){ case"CONVERT_FMT1": outputEle.value=ascii(inputEle.value); break; case"CONVERT_FMT2": outputEle.value=unicode(inputEle.value); break; case"CONVERT_FMT3": outputEle.value=unicode1(inputEle.value); break; case"RECONVERT": outputEle.value=reconvert(inputEle.value); break; } } functionascii(str){ varvalue=''; for(vari=0;i<str.length;i++){ value+='\&#x'+left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';'; } returnvalue; } functionunicode(str){ varvalue=''; for(vari=0;i<str.length;i++){ value+='\\u'+left_zero_4(parseInt(str.charCodeAt(i)).toString(16)); } returnvalue; } functionleft_zero_4(str){ if(str!=null&&str!=''&&str!='undefined'){ if(str.length==2){ return'00'+str; } } returnstr; } functionunicode1(str){ varvalue=''; for(vari=0;i<str.length;i++) value+='&#'+str.charCodeAt(i)+';'; returnvalue; } functionreconvert(str){ str=str.replace(/(\\u)(\w{1,4})/gi,function($0){ return(String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16))); }); str=str.replace(/(&#x)(\w{1,4});/gi,function($0){ returnString.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16)); }); str=str.replace(/(&#)(\d{1,6});/gi,function($0){ returnString.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2"))); }); returnstr; } </script> <style> textarea{ width:100%; height:200px; resize:vertical; border:1pxsolid#CCC; /*border-radius:8px;*/ padding:4px; box-shadow:2px2px5px#d3d6da; -moz-box-shadow:2px2px5px#d3d6da; } </style> 提供一个中文汉字Unicode互转、ASCII与Unicode互转的在线工具,方便帮助你解决中文的乱码问题。 <divclass='divider'></div> <textareaid="input_area"name="input_area"placeholder="贴入要处理的Unicode或Ascii字符"value="">jb51.net-毛票票</textarea> <divclass='row'> <buttononclick="javascript:a('CONVERT_FMT2');">中文汉字转Unicode</button> <buttononclick="javascript:a('RECONVERT');">Unicode转中文汉字</button> <buttononclick="javascript:a('RECONVERT')">ASCII转换Unicode</button> <buttononclick="javascript:a('CONVERT_FMT3');">Unicode转换ASCII</button> <buttononclick="javascript:a('CONVERT_FMT1');">中文转换&#XXXX</button> </div> <textareaname="output_area"id="output_area"onclick="this.select();"placeholder="处理之后的Unicode或Ascii字符"value=""></textarea>
这里就介绍这么多,具体的大家可以多测试一下。
在线Unicode/中文转换工具