title: Converting binary strings to Buffers in Node.js
body: <p>I have a web service that takes a base 64 encoded string representing an image, creates a thumbnail of that image using the imagemagick library, then stores both of them in mongodb. I am doing this with the following code (approximately):</p>
  
  <pre><code>var buf = new Buffer(req.body.data, "base64"); //original image
  
  im.resize({ srcData: buf, width: 256 }, function(err, stdout, stderr) {
      this.thumbnail = new Buffer(stdout, "binary");
      //store buf and stdout in mongo
  });
  </code></pre>
  
  <p>You will notice that I am creating a Buffer object using the "binary" encoding, which the <a href="http://nodejs.org/api/buffer.html#buffer_buffer" rel="nofollow">docs</a> say not to do:</p>
  
  <blockquote>
    <p>'binary' - A way of encoding raw binary data into strings by using
    only the first 8 bits of each character. This encoding method is
    deprecated and should be avoided in favor of Buffer objects where
    possible. This encoding will be removed in future versions of Node.</p>
  </blockquote>
  
  <p>First off I'm not sure what they are saying there. I'm trying to create a Buffer object and they seem to imply I should already have one.</p>
  
  <p>Secondly, the source of the problem appears to be that the imagemagick resize method returns a string containing binary data. Doing typedef(stdout) return "string" and printing it out to the screen certainly appears to show a bunch of non-character data.</p>
  
  <p>So what do I do here? I can't change how imagemagick works. Is there another way of doing what I'm trying to do?</p>