logo 西林@生存 - 骑猪闯天下!
汉字编码转换
2007-1-4 14:56:00 By:xling

做Java的时候,读取资源文件(properties)中的中文,一显示就会成乱码,百度一下,很多人推荐用汉字的Unicode码来表示汉字,还列出一个Java命令,我没有记住。不过不要紧,下面贴出我写的用JavaScript对汉字进行转换的程序,在第一个框里输入汉字,点Convert,或在第一个框里输入转换后的汉字的编码,点Reconvert。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>

转我的贴,写你的名,是一种可耻的行为!请不要让我BS你!
阅读全文 | 回复(1) | 引用通告 | 编辑
Re:汉字编码转换
2007-1-4 20:33:00 By:qicaispace
利用JAVA来选择的吗?
转我的贴,写你的名,是一种可耻的行为!请不要让我BS你!
个人主页 | 引用 | 返回 | 删除 | 回复

发表评论:

    密码:
    主页:
    标题:
    页面数据正在载入...
<<  < 2007 - >  >>
  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
Placard
1,長江後浪推前浪,前浪死在沙灘上!
2,有了钱的男人,才是真正的男人...
3,不要以为把你的小JJ埋进土里,你就rape了整个地球;不要以为把你的小JJ朝向天空,你就rape了整个宇宙!
Logon System
Search
Info about this blog
Others
myDream
bxna 京ICP备05002321号