title: Javascript: Unicode string to hex
body: <p>I'm trying to convert a unicode string to a hexadecimal representation in javascript.</p>
  
  <p>This is what I have: </p>
  
  <pre><code>function convertFromHex(hex) {
      var hex = hex.toString();//force conversion
      var str = '';
      for (var i = 0; i &lt; hex.length; i += 2)
          str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
      return str;
  }
  
  function convertToHex(str) {
      var hex = '';
      for(var i=0;i&lt;str.length;i++) {
          hex += ''+str.charCodeAt(i).toString(16);
      }
      return hex;
  }
  </code></pre>
  
  <p>But if fails on unicode characters, like chinese; </p>
  
  <p>Input: 
  漢字</p>
  
  <p>Output: 
  ªo"[W</p>
  
  <p>Any ideas? Can this be done in javascript? </p>