Table of Contents
Convert Buffer to JSON
Convert JSON to Buffer
Convert Buffer to UTF-8 string
Home Web Front-end JS Tutorial How to convert buffer in Nodejs to JSON format and UTF-8 string?

How to convert buffer in Nodejs to JSON format and UTF-8 string?

Aug 26, 2021 am 10:25 AM
buffer json format node.js

How to convert the buffer in Nodejs to JSON format and UTF-8 string? The following article will introduce to you the method of converting buffer and JSON formats in Nodejs, as well as the method of converting Buffer to UTF-8 string.

How to convert buffer in Nodejs to JSON format and UTF-8 string?

Node.js is different from browser-based JavaScript because Node had a way to handle binary data even before the ES6 draft proposed ArrayBuffer . In Node, the Buffer(buffer) class is the primary data structure used by most I/O operations. It is raw binary data allocated outside the V8 heap and cannot be resized once allocated. [Recommended learning: "nodejs Tutorial"]

Before Node v6.0, to create a new buffer, we only need to use the new keyword to call the constructor Function:

let newBuff = new Buffer('I Love You')
Copy after login

After v6.0, to create a new buffer instance:

let newBuff = Buffer.from('I Love You')
Copy after login

new Buffer() The constructor has been deprecated and replaced by a separate Buffer.from(), Buffer.alloc() and Buffer.allocUnsafe() method replacement.

For more information, you can readOfficial Document.

Convert Buffer to JSON

Buffers can be converted to JSON.

let bufferOne = Buffer.from('All work and no play makes Jack a dull boy')
console.log(bufferOne)

// <Buffer 41 6c 6c 20 77 6f 72 6b 20 61 6e 64 20 6e 6f 20 70 6c 61 79 20 6d 61 6b 65 73 20 4a 61 63 6b 20 61 20 64 75 6c 6c 20 62 6f 79>

let json = JSON.stringify(bufferOne, null, 2)
console.log(json)
/*
{
  "type": "Buffer",
  "data": [
    65,
    108,
    108,
    32,
    119,
    111,
    114,
    107,
    32,
    97,
    110,
    100,
    32,
    110,
    111,
    32,
    112,
    108,
    97,
    121,
    32,
    109,
    97,
    107,
    101,
    115,
    32,
    74,
    97,
    99,
    107,
    32,
    97,
    32,
    100,
    117,
    108,
    108,
    32,
    98,
    111,
    121
  ]
}
*/
Copy after login

JSON Specifies that the type of object to be converted is Buffer and its data.

Convert JSON to Buffer

let bufferOriginal = Buffer.from(JSON.parse(json).data)
console.log(bufferOriginal)

// <Buffer 41 6c 6c 20 77 6f 72 6b 20 61 6e 64 20 6e 6f 20 70 6c 61 79 20 6d 61 6b 65 73 20 4a 61 63 6b 20 61 20 64 75 6c 6c 20 62 6f 79>
Copy after login

Convert Buffer to UTF-8 string

console.log(bufferOriginal.toString(&#39;utf8&#39;)) // All work and no play makes Jack a dull boy
Copy after login

. toString() is not the only way to convert a buffer to a string. Additionally, by default, it is converted to a utf-8 format string.

Another way to convert a buffer to a string is to use the StringDecoder core module in the Node.js API.

string_decoder The module provides a way to decode a Buffer object into a string (to preserve encoded multibyte UTF-8 and UTF -16 character mode) API. An alternative to the above example is as follows:

const { StringDecoder } = require(&#39;string_decoder&#39;)
const decoder = new StringDecoder(&#39;utf8&#39;)

let bufferOriginal = Buffer.from(JSON.parse(json).data)
console.log(decoder.write(bufferOriginal)) // All work and no play makes Jack a dull boy
console.log(decoder.end(bufferOriginal)) // All work and no play makes Jack a dull boy
Copy after login

When the Buffer instance is written to the StringDecoder instance, the internal buffer is used to ensure that the decoded string is not Contains any incomplete multibyte characters. These are kept in the buffer until the next call to stringDecoder.write() or the next call to stringDecoder.end().

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to convert buffer in Nodejs to JSON format and UTF-8 string?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

How to open json format How to open json format Dec 07, 2023 pm 02:28 PM

Opening method: 1. Open with a text editor: You can use any text editor (such as Notepad, Sublime Text, Atom, etc.) to open the JSON file. Just double-click the file name and choose your favorite editor to open it; 2. Open with a web browser: Many modern browsers (such as Chrome, Firefox, Safari, etc.) can directly open JSON files; 3. Use a programming language to parse: JSON files can be opened and parsed using libraries or modules for the corresponding language.

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

See all articles