利用javascript来转换GB2312到UNICONDE -- &#形式

利用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>

posted @ 2007-02-05 16:32  meil  阅读(1149)  评论(2编辑  收藏  举报