🌐
W3Schools
w3schools.com › nodejs › met_buffer_tostring.asp
Node.js buffer toString() Method
Node.js Compiler Node.js Server Node.js Quiz Node.js Exercises Node.js Pratice Problems Node.js Syllabus Node.js Study Plan · ❮ Buffer Module · Display the buffer object as a string: let buf = Buffer.from('abc'); console.log(buf.toString()); Run ...
🌐
Toptal
toptal.com › external-blogs › adeva › convert-node-js-buffer-to-string
How to Convert Node.js Buffer to String | Toptal®
May 26, 2026 - To convert from a string to a Buffer object, use the static Buffer.from() method - again optionally passing the encoding. If you’re a Node.js developer interested in advancing ...
Discussions

node.js - Convert buffer to readable string javascript - Stack Overflow
I receive a JSON as a buffer. I want to parse it into a readable or JSON object. However, despite all techniques (JSON.stringify(), toString('utf8'), I am not able to get it done. More on stackoverflow.com
🌐 stackoverflow.com
node.js - Convert string to buffer Node - Stack Overflow
Copyvar buf = Buffer.from('some string', 'encoding'); ... Save this answer. ... Show activity on this post. Note: Just reposting John Zwinck's comment as answer. One issue might be that you are using a older version of Node (for the moment, I cannot upgrade, codebase struck with v4.3.1). More on stackoverflow.com
🌐 stackoverflow.com
node.js - Converting a nodejs buffer to string and back to buffer gives a different result in some cases - Stack Overflow
A .docXfile is not a UTF-8 string (it's a binary ZIP file) so when you read it into a Buffer object and then call .toString() on it, you're assuming it is already encoding as UTF-8 in the buffer and you want to now move it into a Javascript string. That's not what you have. More on stackoverflow.com
🌐 stackoverflow.com
convert Buffer to String - javascript
pythonjavascriptc#reactjsjavaandroidhtmlflutterc++node.jstypescriptcssrphpangularnext.jsspring-bootmachine-learningsqlexceliosazuredocker More on stackoverflow.com
🌐 stackoverflow.com
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
Unlike 'utf8', each character in the string will be encoded using either 2 or 4 bytes. Node.js only supports the little-endian variant of UTF-16. 'latin1': Latin-1 stands for ISO-8859-1. This character encoding only supports the Unicode characters from U+0000 to U+00FF. Each character is encoded using a single byte. Characters that do not fit into that range are truncated and will be mapped to characters in that range. Converting a Buffer into a string using one of the above is referred to as decoding, and converting a string into a Buffer is referred to as encoding.
🌐
Medium
medium.com › @ryan_forrester_ › buffer-to-string-in-javascript-how-to-guide-b24027575419
Buffer to String in JavaScript (How To Guide) | by ryan | Medium
January 7, 2025 - The buffer is converted to a string with utf8 encoding and displayed. Node.js provides robust support for buffers and their conversion to strings.
🌐
Joshtronic
joshtronic.com › 2022 › 11 › 27 › convert-a-buffer-to-a-string-in-nodejs
Convert a buffer to a string in Node.js - Joshtronic
November 27, 2022 - The buffer is effectively just an array of integers that represent the data that it contains. To get a string back out of the buffer, simply use .toString():
🌐
Medium
medium.com › hackernoon › https-medium-com-amanhimself-converting-a-buffer-to-json-and-utf8-strings-in-nodejs-2150b1e3de57
Converting a Buffer to JSON and Utf8 Strings in Nodejs | by Aman Mittal | HackerNoon.com | Medium
April 1, 2019 - Buffers can convert to JSON. The JSON specifies that the type of object being transformed is a Buffer, and its data. .toString() is not the only way to convert a buffer to a string.
🌐
Educative
educative.io › answers › what-is-the-nodejs-buffertostring-method
What is the Node.js Buffer.toString() method?
In Node.js, the Buffer.toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type.
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer-to-string
Using the Buffer `toString()` Function in Node.js - Mastering JS
August 21, 2020 - const buf = Buffer.from('Hello, World', 'utf8'); buf.toString(); // 'Hello, World' The toString() method's first parameter is a string called encoding. It determines what format Node.js uses to express the raw data.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › node-js-buffer-tostring-method
Node.js Buffer.toString() Method | GeeksforGeeks
October 13, 2021 - Example 1: ... // Node.js program to demonstrate the // Buffer.toString() Method // Creating a buffer var buffer = new Buffer.alloc(5); // Loop to add value to the buffer for (var i = 0; i < 5; i++) { buffer[i] = i + 97; } // Display the value ...
🌐
Melvin George
melvingeorge.me › blog › convert-string-buffer-nodejs
How to convert a String to Buffer and vice versa in Node.js | MELVIN GEORGE
September 19, 2020 - // a string const str = "Hey. this ... as the first argument. ... To convert a Buffer to a String, you can use the toString() in the Buffer object....
🌐
Scaler
scaler.com › home › topics › nodejs › buffers and string decoders in node.js
Buffers and String Decoders in Node.js - Scaler Topics
May 4, 2023 - To convert a buffer into a string, the Node.js StringDecoder is used. ... decoder module implements an API for decoding Buffer objects into strings while preserving encoded multi-byte UTF-8 and UTF-16 characters.
🌐
Leyaa
leyaa.ai › codefly › learn › nodejs › part-1 › nodejs-buffer-to-string-conversion › try
Buffer to string conversion in Node.js - Interactive Code Practice | Leyaa.ai
Node.js · const buf = ... Answer · Attempts: 3 left · 💡 Hint · Common Mistakes · Using toBuffer() which does not exist on Buffer instances....
🌐
DEV Community
dev.to › melvin2016 › how-to-convert-a-string-to-buffer-and-vice-versa-in-node-js-5op
How to convert a String to Buffer and vice versa in Node.js - DEV Community
September 19, 2020 - // a string const str = "Hey. this ... as the first argument. ... To convert a Buffer to a String, you can use the toString() in the Buffer object....
🌐
Daniel Lemire's blog
lemire.me › blog › 2023 › 12 › 08 › fast-buffer-to-string-conversion-in-javascript-with-a-lookup-table
Fast Buffer-to-String conversion in JavaScript with a Lookup Table – Daniel Lemire's blog
December 8, 2023 - When programming in a JavaScript environment such as Node.js, you might recover raw data from the network and need to convert the bytes into strings. In a system such as Node.js, you may represent such raw bytes using a Buffer instance. You can conveniently convert a Buffer instance into a JavaScript (mybuffer.toString()). But, maybe surprisingly, … Continue reading Fast Buffer-to-String conversion in JavaScript with a Lookup Table
Top answer
1 of 1
7

A .docXfile is not a UTF-8 string (it's a binary ZIP file) so when you read it into a Buffer object and then call .toString() on it, you're assuming it is already encoding as UTF-8 in the buffer and you want to now move it into a Javascript string. That's not what you have. Your binary data will likely encounter things that are invalid in UTF-8 and those will be discarded or coerced into valid UTF-8, causing an irreversible change.

What Buffer.toString() does is take a Buffer that is ALREADY encoded in UTF-8 and puts it into a Javascript string. See this comment in the doc,

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, the code you show in your question is wrongly assuming that Buffer.toString() takes binary data and reversibly encodes it as a UTF8 string. That is not what it does and that's why it doesn't do what you are expecting.

Your question doesn't describe what you're actually trying to accomplish. If you want to do something useful with the .docX file, you probably need to actually parse it from it's binary ZIP file form into the actual components of the file in their appropriate format.

Now that you explain you're trying to store it in localStorage, then you need to encode the binary into a string format. One such popular option is Base64 though it isn't super efficient (size wise), but it is better than many others. See Binary Data in JSON String. Something better than Base64 for prior discussion on this topic. Ignore the notes about compression in that other answer because your data is already ZIP compressed.

🌐
DigitalOcean
digitalocean.com › community › tutorials › using-buffers-in-node-js
Using Buffers in Node.js | DigitalOcean
May 1, 2020 - That property tells JavaScript how to convert the object to a primitive data type: boolean, null, undefined, number, string, or symbol. You can read more about Symbols at Mozilla’s JavaScript documentation. Let’s see how we can create a buffer from a string. In the Node.js prompt, enter this: