Compact version of JS (UNICODE->GB) (still contains pinyin and UrlEncode)

Source: Internet
Author: User
Tags compact comparison table contains return version tostring urlencode
encode|js| Pinyin

http://www.csdn.net/Develop/read_article.asp?id=15043
Unicode and GB conversion libraries with the following addresses
Http://www.blueidea.com/user/qswh/qswhU2GB.js
148k JS, local Test no problem, but put on the internet will feel a bit slow

So need a compact version, collections researches is just GB2312 code, that is, part of the GBK, but enough to
This is particularly streamlined code, but the function is unchanged, the difference is that the version can only be used for common Chinese characters.
What horse with what saddle, also modified the UrlEncode and Getspell algorithm, please do not confuse.
This time the qswhgb2312.js is only 18k and can be used freely on the internet

Unicode and GB conversion libraries and UrlEncode and Getspell functions, please download
Http://www.blueidea.com/user/qswh/qswhGB2312.js

The main code is as follows
var strgb= "Ah ... Snore 齄 ";//gb2312 string, slightly
var qswhspell=[" A ", 0,..," Zuo ", 3747];//phonetic comparison table, slightly
function UrlEncode (str) {
 var i,c , p,q,ret= "", strspecial= "!\" #$%& ' () *+,/:;<=>?@[\]^ ' {|} ~%";
 for (i=0;i<str.length;i++) {
  if (str.charcodeat (i) >=0x4e00) {
   var p= Strgb.indexof (Str.charat (i));
   if (p>=0) {
    q=p%94
    p= (P-Q)/94;
    ret+= ("%" + (0xb0+p). ToString () + "%" + (0xa1+q). ToString (). toUpperCase ();
  }
 }
  else{
   C=str.charat (i);
   if (c== "")
    ret+= "+";
   Else if (Strspecial.indexof (c)!=-1)
    ret+= "%" +str.charcodeat (i). ToString (a);
   else
    ret+=c;
 }
 
 return ret;
}

function Getspell (STR,SP) {
var i,c,t,p,ret= "";
if (sp==null) sp= "";
for (i=0;i<str.length;i++) {
if (Str.charcodeat (i) >=0x4e00) {
P=strgb.indexof (Str.charat (i));
if (p>-1&&p<3755) {
for (t=qswhspell.length-1;t>0;t=t-2) if (qswhspell[t]<=p) break;
if (t>0) ret+=qswhspell[t-1]+sp;
}
}
}
Return Ret.substr (0,ret.length-sp.length);
}

The SP is a separator, as the following example
<script src=qswhgb2312.js></script>

<script language=javascript>
document.write (UrlEncode ("Chinese <>\" #%{}|^~[] ' &?+abc ")," <br> ")
document.write (Getspell ("Chinese Programmer's Base"), "<br>"
document.write ("Getspell", "'"), "<br>")
</script>



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.