title: Printing hex/binary strings to a file in nodejs
body: <p>I'm trying to do something seemingly simple:</p>
  
  <pre><code>var token = crypto.randomBytes(32);
  var wstream = fs.createWriteStream('output.hex');
  wstream.write(token);
  wstream.end();
  </code></pre>
  
  <p>The <code>output.hex</code> file is empty after this code is run. <code>token</code> is never modified during the code either.</p>
  
  <p>In the same vein, I try this:</p>
  
  <pre><code>// encrypts a message
  var token = crypto.randomBytes(32);
  var cipher = crypto.createCipher('aes-256-cbc', token);
  var enc_msg = cipher.update(msg, 'binary', 'binary');
  enc_msg += cipher.final('binary');
  
  // print it
  console.log(enc_msg);  // prints Øôâ¨7"ªðqâ~è (binary data - good)
  
  // check the type
  console.log(type of enc_msg); // prints 'string'
  
  // write it to a file (fails)
  var wstream = fs.createWriteStream('output.hex');
  wstream.write(enc_msg);
  wstream.end();
  </code></pre>
  
  <p>What am I missing here? The file is always completely empty.</p>