JS汉字编码 (ascii,unicode)的转换与反转

1ASCII就是编码英文的26个字母和一些常见的符号,之后扩展了一半。总之是一个字节来做编码,大于128的部分是一些特殊符号。但ASCII是无法编码别的东西的,比如说是不存在“中文的ascii码需要2个字符”这种说法的。ASCII就只有一个字节。 2Unicode是足够编码地球上所有的语言了,所以ASCII中所能表示的,Unicode当然全部包括了。Unicode本身是只有2个字节的,之所以出现UTF-8,UTF-16等等之类,那是为了针对不同的应用环境,提高整体编码效率,比如如果某篇文章里绝大部分是英语(单字节就能表示),就比较适合使用utf-8,而如果绝大部分是中文(需要双字节),可能就utf-16比较合适了 3 4下面贴出用JavaScript对汉字进行转换的程序,在第一个框里输入汉字,点Convert,或在第一个框里输入转换后的汉字的编码,点Reconvert。 5 6 7<html xmlns="http://www.w3.org/1999/xhtml"> 8<head> 9<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 10<title>Untitled Document</title> 11<style type="text/css"> 12<!-- 13.btn{ 14font-family: Verdana, Arial, Helvetica, sans-serif; 15font-size: 12px; 16color: #666666; 17text-align: left; 18text-decoration: none; 19display: block; 20overflow: visible; 21margin-right: 10px; 22margin-left: 10px; 23} 24.btn a:hover { 25background-color: #d8dfea; 26border-top-width: 1px; 27border-bottom-width: 1px; 28border-top-style: solid; 29border-bottom-style: solid; 30border-top-color: #333366; 31border-bottom-color: #333366; 32} 33.btn a { 34display: block; 35text-decoration: none; 36color: #666666; 37border-top-width: 1px; 38border-bottom-width: 1px; 39border-top-style: solid; 40border-bottom-style: solid; 41border-top-color: #CCCCCC; 42border-bottom-color: #CCCCCC; 43width: 100px; 44padding-top: 5px; 45padding-right: 10px; 46padding-bottom: 5px; 47padding-left: 30px; 48overflow: visible; 49float: left; 50} 51html { 52font-family: Verdana, Arial, Helvetica, sans-serif; 53font-size: 12px; 54color: #333333; 55line-height: 18px; 56margin: 0px; 57} 58--> 59body{ 60margin:0px; 61} 62</style> 63</head> 64<body> 65<p> 66   <textarea name="source" rows="14" id="source" style="width:99%">中华人民共和国万岁 67中華人民共和國萬歲 68\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD\u4E07\u5C81 69\u4E2D\u83EF\u4EBA\u6C11\u5171\u548C\u570B\u842C\u6B72 70</textarea> 71</p> 72<div class="btn"> 73   <a href="javascript:action('CONVERT_FMT1')"> 74   <strong>Convert</strong><br /> 75   Fmort &amp;#xxxx   </a></div> 76<div class="btn"> 77   <a href="javascript:action('CONVERT_FMT2')"> 78   <strong>Convert</strong><br /> 79Fmort \uxxxx 80   </a>  81</div> 82<div class="btn"> 83   <a href="javascript:action('RECONVERT')"> 84   <strong>ReConvert</strong><br /> 85To &#27721;&#23383; </a>  86</div> 87<p> 88<div id="tt" style="display:none"></div> 89   <textarea name="show2" rows="14" id="show2" style="width:99%"></textarea> 90</p> 91</body> 92</html> 93<script language="javascript" type="text/javascript"> 94var oSource = document.getElementById("source"); 95var oShow2 = document.getElementById("show2"); 96var oTt = document.getElementById("tt"); 97function action(pChoice){ 98switch(pChoice){ 99   case "CONVERT_FMT1": 100    oShow2.value = ascii(oSource.value); 101    break; 102   case "CONVERT_FMT2": 103    oShow2.value = unicode(oSource.value); 104    break; 105   case "RECONVERT": 106    oShow2.value = reconvert(oSource.value); 107    break; 108} 109} 110function ascii(str){ 111return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")}); 112} 113function unicode(str){ 114return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")}); 115} 116function reconvert(str){  117str = str.replace(/(\\u\w{4})/gi,function($0){ 118             return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16))); 119             }); 120             121str = str.replace(/(&#x)(\w{4});/gi,function($0){ 122             return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16)); 123             });             124return str; 125} 126</script>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

CentOS7设置中文字符集

一、字符编码和字符集1、字符编码(characterencoding):字符编码是一种法则,在数字与符号之间建立的对应关系。不同的国家有不同的语言,包含的文字、标点符号、图形符号各有不同。例如在ASCII编码中,用数字97表达字符'a'与字符集相对应,常见的字符编码有AS

Python字符和字符值(ASCII或Unicode码值)转换方法

Python字符和字符值(ASCII或Unicode码值)转换方法(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.cda.cn%2Fview%2F25016.html)这篇文章主要介绍了Python字符和字符值(ASCII或Unicode码值)转换方法,即把字符串在A