
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Convert buffer to readable string in JavaScript?
For this, use the concept of toString(‘utf8’). Following is the code −
In the code below, there are detailed explanations about the buffer.
Example
var actualBufferObject = Buffer.from('[John Smith]', 'utf8') console.log("The actual buffer object="); console.log(JSON.stringify(actualBufferObject)) console.log("Get back the original object="); console.log(actualBufferObject.toString('utf8')); var myObjectValue = '[John Smith]'; console.log("The data you are getting from the buffer is equal to ASCII code equivalent...") for (var counter = 0; counter < myObjectValue.length; counter++) { console.log("The ascii value of " + myObjectValue[counter] + " is =" + (myObjectValue.charCodeAt(counter))); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo197.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo197.js The actual buffer object= {"type":"Buffer","data":[91,74,111,104,110,32,83,109,105,116,104,93]} Get back the original object= [John Smith] The data you are getting from the buffer is equal to ASCII code equivalent... The ascii value of [ is =91 The ascii value of J is =74 The ascii value of o is =111 The ascii value of h is =104 The ascii value of n is =110 The ascii value of is =32 The ascii value of S is =83 The ascii value of m is =109 The ascii value of i is =105 The ascii value of t is =116 The ascii value of h is =104 The ascii value of ] is =93
- Related Articles
- Java Program to convert millisecond to readable string
- How to convert unix timestamp string to readable date in Python?
- How to convert a binary NodeJS Buffer to JavaScript ArrayBuffer?
- How to use string buffer in android?
- How to calculate String Buffer capacity in Java?
- Golang program to clear the string buffer
- Convert UNIX timestamp into human readable format in MySQL?
- Difference between String buffer and String builder in Java
- How to convert Boolean to String in JavaScript?
- How to convert Number to String in JavaScript?
- How to convert String to Number in JavaScript?
- How to convert String to Boolean in JavaScript?
- Convert nested array to string - JavaScript
- JavaScript Convert a string to boolean
- Convert integer array to string array in JavaScript?

Advertisements