The 'binary'
encoding is an alias for 'latin1'
, which you clearly don’t want when reading non-character data.
If you want the raw data, don’t specify an encoding at all (or supply null
)*. You’ll get a Buffer
instead of a string, which you’d then want to use directly rather than using toString
on it.
* (Some APIs [like fs.watch
] also accept 'buffer'
, but it’s not on the list of encodings and readFileSync
doesn’t say it does. [Thanks Patrick for providing the list link.])