title: Reading binary data (pixel color) from a .bmp file in NodeJS
body: <p>I'm trying to read a pixel color (RGB) from a <code>2x2</code> bmp file (72 bytes) where the 1st pixel color is <code>#ffc90e</code>:</p>
  <p><a href="https://i.stack.imgur.com/0EwrL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0EwrL.png" alt="" /></a></p>
  <p>The pixel color (BGR) starts at the position <code>62</code>.</p>
  <p><a href="https://i.stack.imgur.com/Zdngx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zdngx.png" alt="enter image description here" /></a></p>
  <p>So I'm trying to get that pixel color from nodeJS with the code below but I'm getting the wrong hex values. I've already tried changing the <code>readFileSync</code> params to <code>binary</code> and <code>utf-8</code>. With utf-8 it gets closer but not accurate.</p>
  <pre><code>'use strict'
  
  const FS = require('fs') // https://nodejs.org/api/fs.html
  process.chdir(__dirname)
  
  var stream = new Buffer.from(FS.readFileSync('2x2.bmp', 'utf-8'))
  console.log(stream)
  console.log(stream.toJSON())
  
  var blue = stream.toString('hex', 62, 63)
  var green = stream.toString('hex', 64, 65)
  var red = stream.toString('hex', 66, 67)
  
  console.log(red, green, blue)
  </code></pre>
  <p>The output is:</p>
  <p><a href="https://i.stack.imgur.com/saL0Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/saL0Q.png" alt="enter image description here" /></a></p>
  <p>Which may vary if I change <code>readFileSync</code> param to <code>binary</code> or <code>utf-8</code>. But never accurate values are shown, although I can see them with an hex editor @ position <code>62</code> (picture above).</p>
  <p>References:</p>
  <p><a href="https://nodejs.org/api/buffer.html" rel="nofollow noreferrer">https://nodejs.org/api/buffer.html</a></p>
  <p><a href="https://stackoverflow.com/questions/14551608/list-of-encodings-that-node-js-supports">List of encodings that Node.js supports</a></p>
  <p><strong>UPDATE</strong></p>
  <p>I have tried different hex editors and all of them can read the correct binary data. Only node.js can't. Even <a href="https://www.di-mgt.com.au/hexdump-for-windows.html" rel="nofollow noreferrer">this</a> trivial hex editor can read it right but node.js can't:</p>
  <p><a href="https://i.stack.imgur.com/KR4Gx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KR4Gx.png" alt="enter image description here" /></a></p>
  <p>I bet all my coins it is a node.js issue. (v12.16.2)</p>