I'm reading a binary file from my file system using Node.
When I compare the streamed data to a hexdump that I've done, I get different results.
My node code looks like this:
this.readStream = fs.createReadStream(file, {
encoding: 'binary',
});
this.readStream.on('data', (data: string | Buffer) => {
if (!(data instanceof Buffer)) {
data = Buffer.from(data);
}
let dataString = "";
data.forEach(v => {
dataString += v.toString(2).padStart(8, '0')
});
console.log(dataString);
});
The output from above is:
000000000000000000000000000000000000000000000010001000100010001000100010001000100010001000100010001000100010001000100010001000110001100111000010101000100010001100100010001000100010001100100010001000100010001100100010001000100010001000110010001000110010001000100010001000110010001000100010001000110010001000100010001000110000011100100010001000110010001000100010001000110010001000100010001000110010001000100010001000100010001000100011001000100010001000100011001000100010001000100011001000100010001000100011000001011100001010100010001000110010001000100010001000110010001000100010001000110010001000100010001000011100001010110110001000011100001010100010001100100010000111000010101000100011001000100011001000100010001000100011000001110010001000100011001000100010001000100000011000100010001000100011001000100010001000100011110000101010001000100011001000100010001000100011001000100010001000100011001000100010001000100000010001000110001000100011001000100010001000100011001000100010001000100011001000100010001000100011001000100010001100100010001000100010000001100010001000100010001100100010001000100010001100000110001000100010001100100010001000100010001100100010001000100010001100100010001000100010001100100010001000110010001000100010001000110010001000100010001000110010001000100010001000000100010111000010101000100010001100100010001000100010001100100010001000100010001100100010001000100010001100000110001000110010001000100010001000000110001000100010001000110010001000100010001000110000011000100010001000110010001000100010001000110010001000100010001000110010001000100010001000000110001000100011001000100010001000100011001000100010001000100011001000100010001000100011000111101100001010100010001000110010001000100010001000000110001000100010001000100010001000011010001000011100001110100010001000110010001000100010001000110010001000100010001000110010001000100010001000000100011000100010001000110010001000100010001000110010001000100010001000110010001000100010001000110001101000100011001000100010001000100000011000100010001000100011001000100010001000100011000001101100001010100010001000110010001000100010001000110010001000100010001000110010001000100010001000000100010100100011001000100001001000100011001000100001001000100011001000100001001000100011000001011100001010010010001000110010001000010010001000110010001000010010001000110010001000010010001000100010000100100011001000100001001000100000110000111010001000010010001000110010001000010010001000110000010001010010001000110010001000010010001000110010001000010010001000110010001000010010001000000110000100100011001000100001001000100011001000100001001000100011001000100001001000100011000001100001001000100011001000100001001000100000110000111010001000010010001000110010001000010010001000110011000100100011001000100001001000100011001000100001001000100011001000100001001000100000110000111001110111000010100100100010000100100010001010100010001011000010101000100011001000100011001000100001001000100011110000111011111111000011101111100000000000000000110000101000000100000001110000101000001000000010110000101000001100000011110000101000010000000100110000101000010100000101110000101000011000000110110000101000011100000111110000101000100000001000110000101000100100001001110000101000101000001010110000101000101100001100000011010000111000001111000100000001000100010010000100110111111111000011101111111100001110111111110000111011111111000011101111111100001110111111110000111011111111000011101111111100001110111111110000111011111111000011101111111100001110111111110000111011111111000011101111111100001110111100
The Hexdump that I used is: xxd -b -g 0 ${file}
And its output is:
0000000000000000000000000000000000000000000000100010001000100010001000100010001000100010001000100010001000100010001000100010001100011001101000100010001100100010001000100010001100100010001000100010001100100010001000100010001000110010001000110010001000100010001000110010001000100010001000110010001000100010001000110000011100100010001000110010001000100010001000110010001000100010001000110010001000100010001000100010001000100011001000100010001000100011001000100010001000100011001000100010001000100011000001011010001000100011001000100010001000100011001000100010001000100011001000100010001000100001101101100010000110100010001100100010000110100010001100100010001100100010001000100010001100000111001000100010001100100010001000100010000001100010001000100010001100100010001000100010001110100010001000110010001000100010001000110010001000100010001000110010001000100010001000000100010001100010001000110010001000100010001000110010001000100010001000110010001000100010001000110010001000100011001000100010001000100000011000100010001000100011001000100010001000100011000001100010001000100011001000100010001000100011001000100010001000100011001000100010001000100011001000100010001100100010001000100010001100100010001000100010001100100010001000100010000001000101101000100010001100100010001000100010001100100010001000100010001100100010001000100010001100000110001000110010001000100010001000000110001000100010001000110010001000100010001000110000011000100010001000110010001000100010001000110010001000100010001000110010001000100010001000000110001000100011001000100010001000100011001000100010001000100011001000100010001000100011000111101010001000100011001000100010001000100000011000100010001000100010001000100001101000100001111000100010001100100010001000100010001100100010001000100010001100100010001000100010000001000110001000100010001100100010001000100010001100100010001000100010001100100010001000100010001100011010001000110010001000100010001000000110001000100010001000110010001000100010001000110000011010100010001000110010001000100010001000110010001000100010001000110010001000100010001000000100010100100011001000100001001000100011001000100001001000100011001000100001001000100011000001011001001000100011001000100001001000100011001000100001001000100011001000100001001000100010001000010010001100100010000100100010000011100010000100100010001100100010000100100010001100000100010100100010001100100010000100100010001100100010000100100010001100100010000100100010000001100001001000110010001000010010001000110010001000010010001000110010001000010010001000110000011000010010001000110010001000010010001000001110001000010010001000110010001000010010001000110011000100100011001000100001001000100011001000100001001000100011001000100001001000100000110111011001001000100001001000100010101000100010101000100011001000100011001000100001001000100011111111111111111000000000000000001000000100000001100000100000001010000011000000111000010000000100100001010000010110000110000001101000011100000111100010000000100010001001000010011000101000001010100010110000110000001101000011100000111100010000000100010001001000010011011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
I don't believe the difference is because of big or little endian.
I don't think the issue is because of my use of padStart since each unit of the buffer should be 8 bits (1 byte).
The file I'm reading is only 398 bytes
So the question is how do I get Node to read a binary file perfectly from the HDD, and get a binary string representation of the file that is identical to what I would get from a binary dump of the same file.