let fb = fs.readFileSync(filePath); let fileData = fb.toString("utf8"); b = Buffer.from(fileData, "utf8"); console.log(fb); console.log(b); console.log(b == fb); console.log(b.toString("utf8") === fb.toString("utf8"));
I converted buffer to UTF8 string and again tried to get the buffer back using
Buffer.from() but I can see that two buffer are different. However, if I again compare
toString() result of these two buffers they are same.
Output of the above program
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
fileData is not entirely legal UTF8, then per the
If encoding is ‘utf8’ and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character U+FFFD.
So, if you convert to a string and then back to a buffer and the encoding was not perfect utf8 to start with, then the process is not reversible because some illegal utf8 bytes will have been replaced.
.toString('utf8') results will then each contain the same “fixed” version (with the replaced bytes).
The key here is don’t convert a Buffer to a utf8 string unless it’s really legal utf8 data.