Advertisement
KiberInfinity

\uXXXX encode

Apr 7th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    function stringEncode(preescape){   
  2.       var escaped="";
  3.       var i=0;
  4.       for(i=0;i<preescape.length;i++){
  5.          escaped=escaped+encodeCharx(preescape.charAt(i));
  6.       }
  7.       return escaped;
  8.    }           
  9.    function encodeCharx(original){
  10.       var hex=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
  11.       var thechar=original.charCodeAt(0);
  12.       if(thechar>127) {
  13.          var c=thechar;
  14.          var a4=c%16;
  15.          c=Math.floor(c/16);
  16.          var a3=c%16;
  17.          c=Math.floor(c/16);
  18.          var a2=c%16;
  19.          c=Math.floor(c/16);
  20.          var a1=c%16;
  21.          return "\\u"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+"";      
  22.       } else
  23.          return original;
  24.    }
  25.    stringEncode('КириллицаAnySymbols()"[]'); // -> \u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430AnySymbols()"[]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement