title: How to encode and decode using Javascript (Given string length should match Encode string length)
body: <p>I have the following scenario: I need to encode my string then i have to decode my value. </p>
  
  <p>The encoded string should have the same length as the string itself. My input string has only = Alpha &amp; Number values.</p>
  
  <p>For example : </p>
  
  <pre><code>String="Test12345"  (lenght : 9) 
  
  Encode =    (encode should lenght 9) 
  
  Decode = Test12345
  </code></pre>
  
  <p>I tried different CharcodeAt , FromCharCode function but I'm getting longer Encoded strings. I could not find the solution.  </p>
  
  <p>See the output values which i write code:</p>
  
  <pre><code>1. Your Input String: 
  Test12345
  2. Encoded String: 
  4R4A3W3V5Q5P5O5N5M
  3. Result : Decoded/Actual Input String: 
  Test12345
  </code></pre>
  
  <p>code : </p>
  
  <pre><code>String.prototype.toEncodedString = function()
  {
      var ostr=this.toString().replace(/\s+/g,'');
      var x,nstr='',len=ostr.length;
      for(x=0;x&lt;len;++x)
      {
          nstr+=(255-ostr.charCodeAt(x)).toString(36).toUpperCase();  
      };
      return nstr;
  };
  </code></pre>
  
  <p>String.prototype.fromEncodedString = function()
  {</p>
  
  <pre><code>var ostr=this.toString();    
  var x,nstr='',len=ostr.length;
  
  for(x=0;x&lt;len;x+=2) 
  
  {
  
          nstr+=String.fromCharCode(255-parseInt(ostr.substr(x,2),36));
  
  };
  return nstr;
  </code></pre>
  
  <p>};</p>
  
  <p>Could anyone help me on this. </p>