title: Is there a way to convert a string to a base 10 number for encryption?
body: <p>Note: I found a <a href="https://stackoverflow.com/questions/10716796/how-to-convert-a-string-to-its-base-10-representation">similar question</a>, but it is in python.
  <hr>
  I've been trying to think of an algorithm or native method for this, and I'm stumped.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>encode=function(n,f){
      return (n).toString(f)
  }
  decode=function(s,f){
      return parseInt(s,f)
  }
  </code></pre>
  
  <p>Basically, I need a way to convert a string, like <code>'Hello World!'</code> to a base 10 (hexadecimal will work as well) number, like <code>14438792758793754875</code>, and I am wondering if there's a proper way to do it before I possibly waste my time with something like:</p>
  
  <pre><code>str='Hello World'
  returnString=''
  for(var i in str){
      returnString+=(
          str[i]=='A'?'0'
          :str[i]=='B'?'1'
          etc...
      )
  }
  </code></pre>
  
  <p>Not only would that be very time consuming, but I have no idea how I would possibly convert it back, as once I get into numbers like <code>11</code> for <code>L</code>, a for loop wouldn't do, and would return <code>BB</code>.</p>
  
  <p>The reason I am asking this is because I plan to use it in the future for more efficient and secure, encrypted storage. for example, something like:</p>
  
  <pre><code>//save
  numstr=encodeToNumber(inputElement.value)
  b36str=encode(numstr,36)
  storage['items'].push(b36str)
  localStorage['App Data']=JSON.stringify(storage)
  
  //load
  if(localStorage['App Data']){
      storage=JSON.parse(localStorage['App Data'])
  }else{
      storage={
          items:[],
          config:{
              etc
          }
      }
  }
  //Assuming storage.items looks like this: ['rgie', 'zyy', '2il7']
  for(i in storage['items']){
      b36str=storage['items']//rgie, zyy, 2il7
      numstr=decode(b36str,36)//1281110, 46618, 117403
      container.innerHTML+='&lt;hr&gt;'+decodeFromNumber(numstr)//Milk, Eggs, Bread
  }
  
  //Output
  Milk
  Eggs
  Bread
  </code></pre>
  
  <p>I actually did spend several minutes manually encrypting 'Milk', 'Eggs', and 'Bread' to their base36 counterparts so it could be accurate.</p>
  
  <hr>
  
  <p>Update: <code>Aadit M Shaw</code> gave a function that produces hexadecimal strings from strings, and I reverse engineered it to my needs and got this:</p>
  
  <pre><code>en=function(s){
      var s2d=function(s){
          s=s.replace(/ /g,'+').replace(/!/g,'.')
          var n=[]
          for(var i in s){
              n.push(s.charCodeAt(i))
          }
          return n
      }
      var arr=s2d(s)
      var s2=''
      for (var i in arr){
          s2+=arr[i].toString(36)
      }
      return s2
  }
  de=function(s){
      var arr=s.split(/(..)/)
      var arr2=[]
      var s2=''
      for(var i in arr){
          if(arr[i]){
              arr2.push(parseInt(arr[i],36))
          }
      }
      for(var i in arr2){
          s2+=String.fromCharCode(arr2[i])
      }
      return s2.replace(/\+/g,' ')
  }
  </code></pre>
  
  <p>While the encoded string is larger than I would have liked, it could be useful for protecting cookies from being hacked or something.</p>
  
  <p>For example, using the example I made <a href="http://jsfiddle.net/meA7f/" rel="nofollow noreferrer">here</a>, I made this message <code>21173b2x3030172t2p38173d333936172u2p2r2t</code> as well as this <code>ЎǄ̺ǦǄ̌</code>. And if you manage to decode the second one, then decode this too <code>ʒ̴ͻϮ</code></p>
  
  <p>This isn't very practical, though, so I'll probably end up using the library <code>Ragnarokkr</code> linked me to.</p>
  
  <p>Thank you all for the responses!</p>