title: Creating a Blob or a File from JavaScript binary string changes the number of bytes?
body: <p>I have been playing with a few JS encryption libraries (CryptoJS, SJCL) and discovered problems related to the Blob/File APIs and JavaScript "binary strings".</p>
  
  <p>I realized that the encryption isn't even really relevant, so here's a much simplified scenario. Simply read a file in using readAsBinaryString and then create a Blob:</p>
  
  <pre><code>&gt;&gt;&gt; reader.result
  "GIF89a����ÿÿÿÿÿÿ!þCreated with GIMP�,�������D�;"
  &gt;&gt;&gt; reader.result.length
  56
  &gt;&gt;&gt; typeof reader.result
  "string"
  &gt;&gt;&gt; blob = new Blob([reader.result], {type: "image/gif"})
  Blob { size=64, type="image/gif", constructor=function(), more...}
  </code></pre>
  
  <p>I have created a JSFiddle that will basically do the above: it simply reads any arbitrary file, creates a blob from it, and outputs the length vs size:
  <a href="http://jsfiddle.net/6L82t/1/" rel="noreferrer">http://jsfiddle.net/6L82t/1/</a></p>
  
  <p>It appears that, when creating the Blob from the "binary (javascript) string", something with character encoding ends up munging the result.</p>
  
  <p>If a non-binary file is used, you will see that the lengths of the Blob and the original binary string are identical.</p>
  
  <p>So there is something that happens when trying to create a Blob/File from a non-plaintext Javascript string, and I need whatever that is to not happen. I think it may have something to do with the fact that JS strings are UTF-16?</p>
  
  <p>There's a (maybe) related thread here:
  <a href="https://stackoverflow.com/questions/3146483/html5-file-api-read-as-text-and-binary">HTML5 File API read as text and binary</a></p>
  
  <p>Do I need to possibly take the decrypted results (UTF-16) and "convert" them to UTF-8 before putting them in a Blob/File?</p>
  
  <p>Working with someone in #html5 on Freenode, we determined that if you read an ArrayBuffer directly and then create the blob from that by first using a Uint8Array, the bytes work out just fine. You can see a fiddle that essentially does that here:
  <a href="http://jsfiddle.net/GH7pS/4/" rel="noreferrer">http://jsfiddle.net/GH7pS/4/</a></p>
  
  <p>The issue is, at least in my scenario, I am going to end up with a binary string and would like to figure out how to directly convert that into a Blob so that I can then use html5's download to allow the user to click to download the blob directly.</p>
  
  <p>Thanks!</p>