Open In App

Node.js NPM arraybuffer-to-string Module

NPM(Node Package Manager) is a package manager of Node.js packages. There is an NPM package called arraybuffer-to-string used to decode array buffers in actual strings. The package not only converts the buffer to ‘utf8’ string but also it converts the buffer to many forms like base64 encoded string, a hex-encoded string that we use in many contexts .

Command to install:  



npm install arraybuffer-to-string

Syntax to import the package in local file



const arrayBufferToString = require('arraybuffer-to-string')

Syntax to convert arraybuffer to string

arrayBufferToString(buffer, encodingType)

Parameters: It takes two parameter ‘buffer’ and ‘encodingType’ to which we want to convert the arraybuffer as shown below:

Example 1:  This example illustrates how to use ‘arraybuffer-to string’  to convert from array buffer to utf8 string.

Filename – index.js: This file contains logic to convert arraybuffer to utf8 string.




const express = require('express')
const bodyParser = require('body-parser')
const arrayBufferToString =require('arraybuffer-to-string')
const formTemplet = require('./form')
 
const app = express()
const port = process.env.PORT || 3000
 
// The body-parser middleware to parse form data
app.use(bodyParser.urlencoded({extended : true}))
 
// Get route to display HTML form
app.get('/', (req, res) => {
  res.send(formTemplet({}))
})
 
// Post route to handle form submission logic and
app.post('/', (req, res) => {
  const {rawInput} = req.body
  // Creating buffer of string
  const buffer = Buffer.from(rawInput)
   
  // Buffer to array buffer
  var uint8 = new Uint8Array(buffer)
   
  // Converting from array buffer to actual utf8 string
  const utf8str = arrayBufferToString(uint8)
   
  // Printing on console
  console.log(`Original string : ${rawInput}\n`)
  console.log(`Array Buffer : ${uint8}\n`)
  console.log(`Decoded utf8 string : ${utf8str}\n`)
})
 
// Server setup
app.listen(port, () => {
  console.log(`Server start on port ${port}`)
})

filename-form.js: This file contain logic to render the form.




module.exports = ({errors}) => {
  return `
<!DOCTYPE html>
<html>
 
<head>
  <link rel='stylesheet' href=
  <style>
    div.columns {
      margin-top: 100px;
    }
 
    .button {
      margin-top: 10px
    }
  </style>
</head>
 
<body>
  <div class='container'>
    <div class='columns is-centered'>
      <div class='column is-5'>
        <form action='/' method='POST'>
          <div>
            <div>
              <label class='label'
                id='raw-input'>Raw Input
              </label>
            </div>
            <input class='input' type='text'
              name='rawInput' placeholder=
              'Raw Input' for='raw-input'>
          </div>
          <div>
            <button class='button is-info'>
              Submit
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</body>
 
</html>
  `
}

Output:

Submit input string too convert it from string to array-buffer to utf8 encoded string

Response printed in command prompt

Example 2:  This example illustrates how to use ‘arraybuffer-to string’  to convert from array buffer to base64 encoded string.

Filename – index.js: This file contains logic to convert arraybuffer to base64 encoded string.




const express = require('express')
const bodyParser = require('body-parser')
const arrayBufferToString =require('arraybuffer-to-string')
const formTemplet = require('./form')
 
const app = express()
const port = process.env.PORT || 3000
 
// The body-parser middleware to parse form data
app.use(bodyParser.urlencoded({extended : true}))
 
// Get route to display HTML form
app.get('/', (req, res) => {
  res.send(formTemplet({}))
})
 
// Post route to handle form submission logic and
app.post('/', (req, res) => {
  const {rawInput} = req.body
  // Creating buffer of string
  const buffer = Buffer.from(rawInput)
   
  // Buffer to array buffer
  var uint8 = new Uint8Array(buffer)
   
  // Converting from array buffer to actual
  // base64 encoded string
  const base64str = arrayBufferToString(uint8, 'base64')
   
  // Printing on console
  console.log(`Original string : ${rawInput}\n`)
  console.log(`Array Buffer : ${uint8}\n`)
  console.log(`Decoded utf8 string : ${base64str}\n`)
})
 
// Server setup
app.listen(port, () => {
  console.log(`Server start on port ${port}`)
})

filename-form.js : This file contain logic to render the form.




module.exports = ({errors}) => {
  return `
<!DOCTYPE html>
<html>
 
<head>
  <link rel='stylesheet' href=
  <style>
    div.columns {
      margin-top: 100px;
    }
 
    .button {
      margin-top: 10px
    }
  </style>
</head>
 
<body>
  <div class='container'>
    <div class='columns is-centered'>
      <div class='column is-5'>
        <form action='/' method='POST'>
          <div>
            <div>
              <label class='label' id=
                'raw-input'>Raw Input
              </label>
            </div>
            <input class='input' type='text'
              name='rawInput' placeholder=
              'Raw Input' for='raw-input'>
          </div>
          <div>
            <button class='button is-info'>
              Submit
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</body>
 
</html>
  `
}

Output:

Submit input string too convert it from string to array-buffer to base64 encoded string

Response printed in command prompt

Note: We have used some Bulma classes in form.js file to design the content.


Article Tags :