title: Convert binary data to base64 with javascript
body: <p>HTML5 enable you to store data locally which I think it is great. For example here is how you can use it:</p>
  
  <pre><code>        var store = window.localStorage;
          store.setItem('foo', "hellow world");
          var test = store.getItem('foo');
          // test should = "hellow world"
  </code></pre>
  
  <p>In html you can dynamically display an image by settig its source to:</p>
  
  <pre><code>     "data:image/jpg;base64," + (base64string)
  </code></pre>
  
  <p><strong>So my question is</strong> how can I convert binary data to a base64 string so that I can take advantage of html5 local storage?</p>
  
  <p>For example it will be great if I could:</p>
  
  <pre><code>$.ajax({
     url: 'someImage.png',
     type: 'POST',
     success: function (r) {
  
                  // here I want to convert r to a base64 string !
                  // r is not binary so maybe I have to use a different approach
                  var data = ConvertToBase64(r);
  
  
  
                  document.getElementById("img").src = "data:image/png;base64," + data;
              },
  });
  </code></pre>
  
  <hr>
  
  <p>I know I could solve this problem by wrapping the image around a canvas using html5 then converting that to base64string. also I can make a specific service on the server that will send a base64 string data of that image (someImage.aspx).<strong>I just want to know if it will by possible to retrieve binary data from a server and convert that to a base64 string.</strong></p>