首页 > 代码库 > JavaScript编码转换之gb2312转unicode -- &#X形式
JavaScript编码转换之gb2312转unicode -- &#X形式
<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=http://www.mamicode.com/转化>
<script>
var mode="zhuan";
function encode(obj,btn){
if(mode=="zhuan"){
obj.value=http://www.mamicode.com/obj.value.replace(/[^/u0000-/u00FF]/g,function($0){return escape($0).replace(/(%u)(/w{4})/gi,"&#x$2;")});
btn.value="http://www.mamicode.com/还原";
mode="huan";
}else{
obj.value=http://www.mamicode.com/unescape(obj.value.replace(//g,‘%u‘).replace(/;/g,‘‘));
btn.value="http://www.mamicode.com/转化";
mode="zhuan";
}
}
</script></p>
利用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 = http://www.mamicode.com/Str2Unicode(document.all.text.decode.value);
}else{
document.all.text.decode.value = http://www.mamicode.com/Unicode2oStr(document.all.text.encode.value);
}
}else{
if(document.all.text.encode.value!=""){
document.all.text.decode.value = http://www.mamicode.com/Unicode2oStr(document.all.text.encode.value);
}else{
document.all.text.encode.value = http://www.mamicode.com/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="http://www.mamicode.com/确 定">
<input type=reset value="http://www.mamicode.com/清 空">
<input type=button onclick="document.all.text.method.checked?document.all.text.encode.select():document.all.text.decode.select()" value="http://www.mamicode.com/全 选">
</form>
JavaScript编码转换之gb2312转unicode -- &#X形式