I realize this is similar to other questions that have been asked, but after pouring over stack overflow and node.js documentation for several days I decided it was time to ask for help.
Basically what I need to do is read data from a binary file and extract information to then convert into plain text.
I have been trying to use buffers but I am not seeing any progress.
If someone could get me to a point where you can read the entire file, and decode into plain text I believe I can take it from there.
const fs = require('fs');
let buf = fs.readFileSync(__dirname + '/sd_bl.bin');
let buffer = new Buffer.alloc(Buffer.byteLength(buf));
buffer.write(buf.toString('base64'));
console.log(buffer);
This is what I currently have, and the output is:
<Buffer 41 41 45 43 41 77 51 46 42 67 63 41 41 51 49 44 42 41 55 47 42 77 41 42 41 67 4d 45 42 51 59 48 41 41 45 43 41 77 51 46 42 67 63 41 41 51 49 44 42 41 ... 7950 more bytes>
So it seems to be allocating and writing to the buffer correctly, but I don't know what the next steps should be as far as getting plain text out of it.
I am relatively new here, but here's a shot! I believe your console.log statement may be misleading you.
Console-logging buffer.toString()
instead, should be sufficient to get you the human-readable contents of the file.
Here is an example:
var buffer = fs.readFileSync('yourfile.txt')
console.log(buffer.toString())
Currently, your code creates a new buffer object (buffer
) after reading the file, writes the contents of the old buffer to it and then prints out the new Buffer object itself instead of its string representation.
Edit: Screenshot of this in Terminal
For more on actually reading the file line-by-line, I would check this other so post out: Read a file one line at a time in node.js?