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 OverflowNode.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.
Top answer 1 of 5
251
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.
2 of 5
19
You can use Buffer.from() to convert a string to buffer. More information on this can be found here
var buf = Buffer.from('some string', 'encoding');
for example
var buf = Buffer.from(bStr, 'utf-8');
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
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
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
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
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.
Futurestud.io
futurestud.io › tutorials › node-js-convert-a-string-to-buffer
Node.js — Convert a String to Buffer - Future Studio
June 22, 2023 - const buffer = Buffer.from('your string value', 'utf8')
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.
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');
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.
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]).