Welcome

首页 / 脚本样式 / JavaScript / js 中文汉字转Unicode、Unicode转中文汉字、ASCII转换Unicode、Unicode转换ASCII、中文转换&#XXX函数代码

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