You can do:

var buf = Buffer.from(bufStr, 'utf8');

But this is a bit silly, so another suggestion would be to copy the minimal amount of code out of the called function to allow yourself access to the original buffer. This might be quite easy or fairly difficult depending on the details of that library.

Answer from John Zwinck on Stack Overflow
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
For converting between strings and Buffers, typically 'utf8' is the right choice. 'ucs2', 'ucs-2': Aliases of 'utf16le'. UCS-2 used to refer to a variant of UTF-16 that did not support characters that had code points larger than U+FFFF. In Node.js, these code points are always supported. import { Buffer } from 'node:buffer'; Buffer.from('1ag123', 'hex'); // Prints <Buffer 1a>, data truncated when first non-hexadecimal value // ('g') encountered.
Discussions

Goodbye, Node.js Buffer
My biggest use of Buffer is for string encoding. It saves me a ton of trouble in converting to hexadecimal, binary and UTF-8. I don't really want to have to implement those conversions (did the work on a base32 encoding for TOTP once and I don't want to do it again) Edit: string encoding, not strong encoding More on reddit.com
🌐 r/node
22
98
October 24, 2023
JS: How to convert ArrayBuffer to string?
Convert it to a Uint8Array, then convert that to a base64 string with btoa: https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string More on reddit.com
🌐 r/learnprogramming
1
1
May 2, 2023
What is buffer in Node js?
A buffer is simply a representation of binary data. Everything in nodejs, from a string, to a bool, can be watered down into 1’s and 0’s. A buffer lets you take binary, a string, or a number, and then turn it into bits. Buffers are used with the Filesystem (“fs”), HTTP requests/servers, and more commonly in “streams”. Edit: streams take a buffer (or, another stream) and break it into a bunch of tiny buffers. When doing stuff like compression, encryptions, etc. then the stream will make sure only a small buffer is calculated at a time, sending in the small buffers in one at a time. More on reddit.com
🌐 r/node
38
49
July 26, 2020
Convert String to base64
Buffer.from creates a buffer (aka raw binary data), not an encoded string, the argument that you provide is the encoding the string is currently in. In order to change the encoding, you have to encode it again: Buffer.from("some string", "utf8").toString("base64") Similar to reverse the process: Buffer.from("c29tZSBzdHJpbmc=", "base64").toString("utf8") More on reddit.com
🌐 r/learnjavascript
5
5
March 6, 2023
🌐
W3Schools
w3schools.com › nodejs › met_buffer_tostring.asp
Node.js buffer toString() Method
The toString() method returns the buffer object according to the specified encoding.
🌐
Adeva
adevait.com › nodejs › convert-node-js-buffer-to-string
How to Convert Node.js Buffer to String | Toptal®
May 26, 2026 - You can also convert data in the opposite direction by creating a new Buffer from a string.
🌐
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 - To convert a string to a Buffer, you can use the from() method from the global Buffer class in Node.js.
🌐
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 - To convert a string to a Buffer, you can use the from() method from the global Buffer class in Node.js.
Find elsewhere
🌐
W3Schools
w3schools.com › nodejs › met_buffer_from.asp
Node.js Buffer.from() Method
The Buffer.from() method creates a new buffer filled with the specified string, array, or buffer.
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer-to-string
Using the Buffer `toString()` Function in Node.js - Mastering JS
August 21, 2020 - By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer.from(), the toString() function gives you the original string back.
🌐
Node.js
nodejs.org › download › release › v9.10.1 › docs › api › buffer.html
Buffer | Node.js v9.10.1 Documentation
Buffer.from(string[, encoding]) returns a new Buffer that contains a copy of the provided string.
🌐
Node.js
nodejs.org › download › release › v10.2.0 › docs › api › buffer.html
Buffer | Node.js v10.2.0 Documentation
Buffer.from(string[, encoding]) returns a new Buffer that contains a copy of the provided string.
🌐
LogRocket
blog.logrocket.com › home › node.js buffer: a complete guide
Node.js buffer: A complete guide - LogRocket Blog
June 4, 2024 - The buffer.from() method enables you to create a new buffer from any object, like strings, buffer, arrays, and ArrayBuffer().
🌐
Educative
educative.io › answers › what-is-the-nodejs-buffertostring-method
What is the Node.js Buffer.toString() method?
Converting a buffer to a string ... a string to a buffer is known as decoding. ... encoding: This is the format in which the buffer data has to be encoded. The encodings supported by Node.js include utf8, utf16le, and latin1.
🌐
Node.js
nodejs.org › download › release › v4.1.2 › docs › api › buffer.html
Buffer Node.js v4.1.2 Manual & Documentation
Converting between Buffers and JavaScript string objects requires an explicit encoding method.
🌐
TutorialsPoint
tutorialspoint.com › nodejs › nodejs_buffers.htm
Node.js - Buffers
// Creates a zero-filled Buffer of length 10. const buf1 = Buffer.alloc(10); // Creates an uninitialized buffer of length 10. const buf2 = Buffer.allocUnsafe(10); // Creates a Buffer containing array const buf3 = Buffer.from([1, 2, 3]); // creates a buffer from string const buf4 = Buffer.from('hello world', 'utf8');
🌐
Dustin John Pfister
dustinpfister.github.io › 2019 › 07 › 19 › nodejs-buffer-from
New Buffer from strings arrays and more in nodejs | Dustin John Pfister at github pages
March 19, 2021 - In any case when creating a nodejs project buffers do come up now and then, and the from method of the Buffer global comes in handy when there is a desire to quickly create a buffer with an initial value derives from a hex string for example.
🌐
Codegive
codegive.com › blog › js_buffer_from_string.php
Mastering <code>js buffer from string</code> (2024): Unlock Binary Data Power & Performance Secrets
For instance, the string "hello" will be represented differently in bytes depending on whether it's encoded as 'utf8', 'ascii', or 'base64'. The primary method for this conversion is Buffer.from(string, [encoding]).
🌐
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 - Example: Let us begin by importing the StringDecoder module, then build an object by supplying the utf-8 encoding to the StringDecoder's constructor’s parameter. The write() method is invoked using this object. Using the Buffer.from() method, you can create a new buffer.