0

(A beginner in Node JS.) I am reading a file in node js using fs.readFileSync(filename) which returns a Buffer.

I've got two linked questions:

  1. I am guessing the program sees binary (0 & 1) data, and converts it to symbols once tell it how to encode the binary data. Is this correct?

For example, when the file is read, it returns the information in Hex codes, and can be transformed by reading it with the appropriated encoding, for example utf-8, if we know it.

  1. The file I am reading uses readUint8() from Buffer. An unsigned integer 8 bits long.

What encode will this use to transform the first 8 bits to a number?


Are there simple exercises to write a decoder for a very simple encoded file?

1 Answer 1

0

What encode will this use to transform the first 8 bits to a number?

It's not really an encoding. The data in the Buffer is binary (a sequence of raw bytes). The code behind buffer.readUint8() knows how to grab a single byte from the buffer and convert it to the internal format that Javascript uses for a Number. In this specific case, it's taking an 8-bit value from the buffer, treating it as an unsigned value and converted it to the double floating point format that a Javascript Number uses where it can then be used as a number in Javascript.

This is presumably native code behind that method doing this because it's bit manipulation (moving bits around) to change formats and end up with a Javascript Number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.