JavaScript transcoding conversion gb2312 to Unicode--& #X形式

Source: Internet
Author: User

http://www.cnblogs.com/meil/archive/2007/01/31/635936.html JavaScript encoding conversion gb2312 to Unicode 1.<title>gb2312 Unicode Conversion Tool </title>
<div align=center>
<center>
<table border=0 cellpadding=0 cellspacing=0 style= "Border-collapse:collapse" width=600 id=AutoNumber1 height=26 >
<tr>
&LT;TD width=100% height=26>
<p align=center><font face= blackbody size=5 color= #FF0000 >unicode Conversion Tool </font></p>
<p> How to use:</p>
<p> enter Chinese text in the text box below and press "convert" to convert it to Unicode characters. </p>
<p> then press restore to restore it to Simplified Chinese. </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= conversion >
<script>
var mode= "Zhuan";
function encode (OBJ,BTN) {
if (mode== "Zhuan") {
Obj.value=obj.value.replace (/[^\u0000-\u00ff]/g,function) {return Escape ($). Replace (/(%u) (\w{4})/gi, "& #x ")});
btn.value= "Restore";
Mode= "Huan";
}else{
Obj.value=unescape (Obj.value.replace (/& #x/g, '%u '). Replace (/;/g, '));
Btn.value= "Transformation";
Mode= "Zhuan";
}
}
</script></p> 2.

Using JavaScript to transform

<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 is designed and writen by Windy_sk <[email protected]>
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>
Text prototype:<br>
<textarea name= "decode" cols= "rows=" ></textarea>
<br>
Convert Code:<br>
<textarea name= "encode" cols= "rows=" ></textarea>
<br>
<input type= "checkbox" Name= "method" checked> forward conversion
<input Type=button onclick= "Modi_str ()" value= "OK" >
<input type=reset value= "Empty" >
<input Type=button onclick= "Document.all.text.method.checked?document.all.text.encode.select (): Document.all.text.decode.select () "value=" Select All >
</form>

JavaScript transcoding conversion gb2312 to Unicode--& #X形式

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.