码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript编码转换之gb2312转unicode -- &#X形式

时间:2014-11-04 12:24:39      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   使用   

http://www.cnblogs.com/meil/archive/2007/01/31/635936.html JavaScript编码转换之gb2312转unicode
 
 
1.
<html>
<head>
<title>gb2312 unicode转换工具</title>
<div align=center>
<center>
<table border=0 cellpadding=0 cellspacing=0 style="border-collapse: collapse" width=600 id=AutoNumber1 height=26>
  <tr>
    <td width=100% height=26>
    <p align=center><font face=黑体 size=5 color=#FF0000>unicode  转换工具</font></p>
    <p>使用方法:</p>
    <p>  在下面的文本框中输入中文文字,按“转化”,即可将其转化为unicode字符。</p>
    <p>  再按“还原”,即可将其还原为简体中文。</td>
  </tr>
</table>
</center>
</div>
<p align=center>
<textarea cols=82 rows=10 id=code>
</textarea> </p>
<p align=center>

<input type=button onclick=encode(code,this) value=转化>
<script>
var mode="zhuan";
function encode(obj,btn){
   if(mode=="zhuan"){
       obj.value=obj.value.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});
       btn.value="还原";
       mode="huan";
   }else{
       obj.value=unescape(obj.value.replace(/&#x/g,‘%u‘).replace(/;/g,‘‘));
       btn.value="转化";
       mode="zhuan";
   }
}
</script></p>
 
 
2.

利用javascript来转换

<style>
BODY {
FONT-SIZE: 9pt; PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px;
}
input {
FONT-SIZE: 9pt; height: 13pt;
}
</style>

<script language="JavaScript1.2">
/*
This following code are designed and writen by Windy_sk <seasonx@163.net>
You can use it freely, but u must held all the copyright items!
*/

function Str2Unicode(str){
var arr = new Array();
for(var i=0;i<str.length;i++){
  arr[i]="&#" + str.charCodeAt(i) + ";";
}
return(arr.toString().replace(/,/g,""));
}

function Unicode2oStr(str){
var re=/&#[\da-fA-F]{1,5};/ig;
var arr=str.match(re);
if(arr==null)return("");
for(var i=0;i<arr.length;i++){
  arr[i]=String.fromCharCode(arr[i].replace(/[&#;]/g,""));
}
return(arr.toString().replace(/,/g,""))
}

function modi_str(){
if(document.all.text.method.checked){
  if(document.all.text.decode.value!=""){
   document.all.text.encode.value = Str2Unicode(document.all.text.decode.value);
  }else{
   document.all.text.decode.value = Unicode2oStr(document.all.text.encode.value);
  }
}else{
  if(document.all.text.encode.value!=""){
   document.all.text.decode.value = Unicode2oStr(document.all.text.encode.value);
  }else{
   document.all.text.encode.value = Str2Unicode(document.all.text.decode.value);
  }
}
}
</script>
<title>Unicode</title>
<form name=text>
文本原型:<br>
  <textarea name="decode" cols="100" rows="10"></textarea>
  <br>
转换代码:<br>
  <textarea name="encode" cols="100" rows="10"></textarea>
  <br>
  <input type="checkbox" name="method" checked> 正向转换
  <input type=button onclick="modi_str()" value=" 确 定 ">
  <input type=reset value=" 清 空 ">
  <input type=button onclick="document.all.text.method.checked?document.all.text.encode.select():document.all.text.decode.select()" value=" 全 选 ">
</form>

 
 
 

JavaScript编码转换之gb2312转unicode -- &#X形式

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/svennee/p/4073060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!