title: Converting Image Hex to base64 in JavaScript
body: <p>I'm looking for a way to convert a hex image (for example a jpg in hex form) back to base64 in order to display the image in a web page.</p>
  
  <p>I have been using the code below for smaller images but it fails for larger images with a max call stack exceeded error.</p>
  
  <p><strong>src</strong> below is the source of the image in hex format.</p>
  
  <pre><code>test.img = new Image();
  test.img.src = "data:image/jpg; base64," + hexToBase64(src);
  
  function hexToBase64(str) {
      return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
  }
  </code></pre>