Ext,UTF8 函數

來源:互聯網
上載者:User

 

Ext,UTF8 函數在ExtJs 論壇看到,害怕以後忘記了,先記下了.
  1. var Ext.util.Utf8 = {   
  2.   
  3.     // public method for url encoding   
  4.     encode : function (string) {   
  5.         string = string.replace(/rn/g,"/n");   
  6.         var utftext = "";   
  7.   
  8.         for (var n = 0; n < string.length; n++) {   
  9.   
  10.             var c = string.charCodeAt(n);   
  11.   
  12.             if (c < 128) {   
  13.                 utftext += String.fromCharCode(c);   
  14.             }   
  15.             else if((c > 127) && (c < 2048)) {   
  16.                 utftext += String.fromCharCode((c >> 6) | 192);   
  17.                 utftext += String.fromCharCode((c & 63) | 128);   
  18.             }   
  19.             else {   
  20.                 utftext += String.fromCharCode((c >> 12) | 224);   
  21.                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);   
  22.                 utftext += String.fromCharCode((c & 63) | 128);   
  23.             }   
  24.   
  25.         }   
  26.   
  27.         return utftext;   
  28.     },   
  29.   
  30.     // public method for url decoding   
  31.     decode : function (utftext) {   
  32.         var string = "";   
  33.         var i = 0;   
  34.         var c = c1 = c2 = 0;   
  35.   
  36.         while ( i < utftext.length ) {   
  37.   
  38.             c = utftext.charCodeAt(i);   
  39.   
  40.             if (c < 128) {   
  41.                 string += String.fromCharCode(c);   
  42.                 i++;   
  43.             }   
  44.             else if((c > 191) && (c < 224)) {   
  45.                 c2 = utftext.charCodeAt(i+1);   
  46.                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));   
  47.                 i += 2;   
  48.             }   
  49.             else {   
  50.                 c2 = utftext.charCodeAt(i+1);   
  51.                 c3 = utftext.charCodeAt(i+2);   
  52.                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));   
  53.                 i += 3;   
  54.             }   
  55.   
  56.         }   
  57.   
  58.         return string;   
  59.     }   
  60. }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.