js 中文汉字、Unicode、ASCII互相转换函数代码

您所在的位置:网站首页 字符串转为ascii js 中文汉字、Unicode、ASCII互相转换函数代码

js 中文汉字、Unicode、ASCII互相转换函数代码

2024-07-16 00:02| 来源: 网络整理| 查看: 265

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互转;默认情况下采用十六进制。

下面函数都需要用到的函数

? 1 2 3 4 5 6 7 8 function left_zero_4(str) {              if (str != null && str != '' && str != 'undefined' ) {                  if (str.length == 2) {                      return '00' + str;                  }              }              return str;          }

中文汉字转Unicode

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 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

? 1 2 3 4 5 6 function unicode1(str){       var value= '' ;      for ( var i = 0; i < str.length; i++)          value += '&#' + str.charCodeAt(i) + ';' ;      return value; }

中文转换&#XXXX

? 1 2 3 4 5 6 7 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; }

完整的可以测试的代码

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                   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;           }             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; } 提供一个中文汉字Unicode互转、 ASCII与Unicode互转的在线工具,方便帮助你解决中文的乱码问题。                  jb51.net - 脚本之家                               中文汉字转Unicode                  Unicode转中文汉字                        ASCII转换Unicode                  Unicode转换ASCII                  中文转换& #XXXX             

这里就介绍这么多,具体的大家可以多测试一下。

在线Unicode/中文转换工具

原文地址http://www.jb51.net/article/99274.htm



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3