当前位置:
首页
文章
前端
详情

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

ASCII就是编码英文的26个字母和一些常见的符号,之后扩展了一半。总之是一个字节来做编码,大于128的部分是一些特殊符号。但ASCII是无法编码别的东西的,比如说是不存在“中文的ascii码需要2个字符”这种说法的。ASCII就只有一个字节。 
Unicode是足够编码地球上所有的语言了,所以ASCII中所能表示的,Unicode当然全部包括了。Unicode本身是只有2个字节的,之所以出现UTF-8,UTF-16等等之类,那是为了针对不同的应用环境,提高整体编码效率,比如如果某篇文章里绝大部分是英语(单字节就能表示),就比较适合使用utf-8,而如果绝大部分是中文(需要双字节),可能就utf-16比较合适了

下面贴出用JavaScript对汉字进行转换的程序,在第一个框里输入汉字,点Convert,或在第一个框里输入转换后的汉字的编码,点Reconvert。


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.btn{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #666666;
text-align: left;
text-decoration: none;
display: block;
overflow: visible;
margin-right: 10px;
margin-left: 10px;
}
.btn a:hover {
background-color: #d8dfea;
border-top-width: 1px;
border-bottom-width: 1px;
border-top-style: solid;
border-bottom-style: solid;
border-top-color: #333366;
border-bottom-color: #333366;
}
.btn a {
display: block;
text-decoration: none;
color: #666666;
border-top-width: 1px;
border-bottom-width: 1px;
border-top-style: solid;
border-bottom-style: solid;
border-top-color: #CCCCCC;
border-bottom-color: #CCCCCC;
width: 100px;
padding-top: 5px;
padding-right: 10px;
padding-bottom: 5px;
padding-left: 30px;
overflow: visible;
float: left;
}
html {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
line-height: 18px;
margin: 0px;
}
-->
body{
margin:0px;
}
</style>
</head>
<body>
<p>
   <textarea name="source" rows="14" id="source" style="width:99%">中华人民共和国万岁
中華人民共和國萬歲
\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD\u4E07\u5C81
\u4E2D\u83EF\u4EBA\u6C11\u5171\u548C\u570B\u842C\u6B72
</textarea>
</p>
<div class="btn">
   <a href="javascript:action('CONVERT_FMT1')">
   <strong>Convert</strong><br />
   Fmort &amp;#xxxx   </a></div>
<div class="btn">
   <a href="javascript:action('CONVERT_FMT2')">
   <strong>Convert</strong><br />
Fmort \uxxxx
   </a> 
</div>
<div class="btn">
   <a href="javascript:action('RECONVERT')">
   <strong>ReConvert</strong><br />
To &#27721;&#23383; </a> 
</div>
<p>
<div id="tt" style="display:none"></div>
   <textarea name="show2" rows="14" id="show2" style="width:99%"></textarea>
</p>
</body>
</html>
<script language="javascript" type="text/javascript">
var oSource = document.getElementById("source");
var oShow2 = document.getElementById("show2");
var oTt = document.getElementById("tt");
function action(pChoice){
switch(pChoice){
   case "CONVERT_FMT1":
    oShow2.value = ascii(oSource.value);
    break;
   case "CONVERT_FMT2":
    oShow2.value = unicode(oSource.value);
    break;
   case "RECONVERT":
    oShow2.value = reconvert(oSource.value);
    break;
}
}
function ascii(str){
return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")});
}
function unicode(str){
return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")});
}
function reconvert(str){ 
str = str.replace(/(\\u\w{4})/gi,function($0){
             return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16)));
             });
            
str = str.replace(/(&#x)(\w{4});/gi,function($0){
             return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16));
             });            
return str;
}
</script>

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。