You can use Buffer.from(string[, encoding]). The Buffer class has implemented the Uint8Array interface in Node since v4.x. You can also optionally specify an encoding with which to do string processing in both directions, i.e. buffer.toString([encoding]).

Answer from Patrick Roberts on Stack Overflow
🌐
haikel-fazzani
haikel-fazzani.eu.org › snippet › string-uint8array-conversion
Converting String to Uint8Array & Vice-Versa
October 23, 2024 - const str = 'Hello, World!'; const encoder = new TextEncoder(); const uint8Array = encoder.encode(str); console.log(uint8Array); // Output: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33] ... Simple and concise.
Discussions

Uint8Array to string in Javascript - Stack Overflow
If we want to convert back to a Uint8Array from a string, then we'd do this: ... Be aware that if you declared an encoding like base64 when converting to a string, then you'd have to use Buffer.from(str, "base64") if you used base64, or whatever other encoding you used. This will not work in the browser without a module! NodeJS ... More on stackoverflow.com
🌐 stackoverflow.com
Convert string to Uint8Array in javascript - Stack Overflow
I have a string "ab05d705" and I am trying to convert it to the following so I can add it to a Uint8Array. So how do I convert the string "ab05d705" to 0xab,0x05,0xd7,0x05 to put into the foll... More on stackoverflow.com
🌐 stackoverflow.com
Convert Uint8Array into hex string equivalent in Node.js - Stack Overflow
Second method works only for an Array but not Uint8Array, as the map function tries to write a string to the array (for Uint8Array it is casted to a number and evaluates to zero when there is a letter in the hex number). Workaround is to use Array.from(uint8) instead of uint8. More on stackoverflow.com
🌐 stackoverflow.com
Uint8Array Buffer / String conversion in javascript
🌐 forum.safedev.org
October 19, 2017
🌐
npm
npmjs.com › package › uint8arrays
uint8arrays - npm
May 7, 2026 - Returns a new Uint8Array created from the passed string and interpreted as the passed encoding.
      » npm install uint8arrays
    
Published   May 07, 2026
Version   6.1.1
🌐
codelessgenie
codelessgenie.com › blog › how-convert-a-string-to-type-uint8array-in-nodejs
How to Convert a String to Uint8Array in Node.js: Simple Methods Without Custom Parsers — CodeLessGenie.com
Solution: Remember: All Buffers are Uint8Array instances. You can pass a Buffer directly to functions expecting a Uint8Array. Problem: Passing invalid hex (e.g., odd-length strings) or base64 (invalid characters) to Buffer.from(). Solution: Validate inputs first.
🌐
Bun
bun.com › guides › streams › node-readable-to-uint8array
Convert a Node.js Readable to an Uint8Array | Bun Examples
To convert a Node.js Readable stream to an Uint8Array in Bun, you can create a new Response object with the stream as the body, then use bytes() to read the stream into an Uint8Array.
🌐
Bun
bun.sh › guides › streams › node-readable-to-uint8array
Convert a Node.js Readable to an Uint8Array - Bun
To convert a Node.js Readable stream to an Uint8Array in Bun, you can create a new Response object with the stream as the body, then use bytes() to read the stream into an Uint8Array.
Find elsewhere
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.5.1 Documentation
value <string> | <Buffer> | <Uint8Array> | <integer> The value with which to fill buf.
Top answer
1 of 16
352

Update 2016 - five years on there are now new methods in the specs (see support below) to convert between strings and typed arrays using proper encoding.

##TextEncoder

The TextEncoder represents:

The TextEncoder interface represents an encoder for a specific method, that is a specific character encoding, liarraybufferke utf-8, iso-8859-2, koi8, cp1261, gbk, ... An encoder takes a stream of code points as input and emits a stream of bytes.

Change note since the above was written: (ibid.)

Note: Firefox, Chrome and Opera used to have support for encoding types other than utf-8 (such as utf-16, iso-8859-2, koi8, cp1261, and gbk). As of Firefox 48 [...], Chrome 54 [...] and Opera 41, no other encoding types are available other than utf-8, in order to match the spec.*

*) Updated specs (W3) and here (whatwg).

After creating an instance of the TextEncoder it will take a string and encode it using a given encoding parameter:

if (!("TextEncoder" in window)) 
  alert("Sorry, this browser does not support TextEncoder...");

var enc = new TextEncoder(); // always utf-8
console.log(enc.encode("This is a a string to be converted to a Uint8Array"));

You then of course use the .buffer parameter on the resulting Uint8Array to convert the underlaying ArrayBuffer to a different view if needed.

Just make sure that the characters in the string adhere to the encoding schema, for example, if you use characters outside the UTF-8 range in the example they will be encoded to two bytes instead of one.

For general use you would use UTF-16 encoding for things like localStorage.

##TextDecoder

Likewise, the opposite process uses the TextDecoder:

The TextDecoder interface represents a decoder for a specific method, that is a specific character encoding, like utf-8, iso-8859-2, koi8, cp1261, gbk, ... A decoder takes a stream of bytes as input and emits a stream of code points.

All available decoding types can be found here.

if (!("TextDecoder" in window))
  alert("Sorry, this browser does not support TextDecoder...");

var enc = new TextDecoder("utf-8");
var arr = new Uint8Array([84,104,105,115,32,105,115,32,97,32,85,105,110,116,
                          56,65,114,114,97,121,32,99,111,110,118,101,114,116,
                          101,100,32,116,111,32,97,32,115,116,114,105,110,103]);
console.log(enc.decode(arr));

##The MDN StringView library

An alternative to these is to use the StringView library (licensed as lgpl-3.0) which goal is:

  • to create a C-like interface for strings (i.e., an array of character codes — an ArrayBufferView in JavaScript) based upon the JavaScript ArrayBuffer interface
  • to create a highly extensible library that anyone can extend by adding methods to the object StringView.prototype
  • to create a collection of methods for such string-like objects (since now: stringViews) which work strictly on arrays of numbers rather than on creating new immutable JavaScript strings
  • to work with Unicode encodings other than JavaScript's default UTF-16 DOMStrings

giving much more flexibility. However, it would require us to link to or embed this library while TextEncoder/TextDecoder is being built-in in modern browsers.

#Support

As of July/2018:

TextEncoder (Experimental, On Standard Track)

 Chrome    | Edge      | Firefox   | IE        | Opera     | Safari
 ----------|-----------|-----------|-----------|-----------|-----------
     38    |     ?     |    19°    |     -     |     25    |     -

 Chrome/A  | Edge/mob  | Firefox/A | Opera/A   |Safari/iOS | Webview/A
 ----------|-----------|-----------|-----------|-----------|-----------
     38    |     ?     |    19°    |     ?     |     -     |     38

°) 18: Firefox 18 implemented an earlier and slightly different version
of the specification.

WEB WORKER SUPPORT:

Experimental, On Standard Track

 Chrome    | Edge      | Firefox   | IE        | Opera     | Safari
 ----------|-----------|-----------|-----------|-----------|-----------
     38    |     ?     |     20    |     -     |     25    |     -

 Chrome/A  | Edge/mob  | Firefox/A | Opera/A   |Safari/iOS | Webview/A
 ----------|-----------|-----------|-----------|-----------|-----------
     38    |     ?     |     20    |     ?     |     -     |     38

Data from MDN - `npm i -g mdncomp` by epistemex
2 of 16
207

Although Dennis and gengkev solutions of using Blob/FileReader work, I wouldn't suggest taking that approach. It is an async approach to a simple problem, and it is much slower than a direct solution. I've made a post in html5rocks with a simpler and (much faster) solution: http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String

And the solution is:

function ab2str(buf) {
  return String.fromCharCode.apply(null, new Uint16Array(buf));
}

function str2ab(str) {
  var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
  var bufView = new Uint16Array(buf);
  for (var i=0, strLen=str.length; i<strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}

EDIT:

The Encoding API helps solving the string conversion problem. Check out the response from Jeff Posnik on Html5Rocks.com to the above original article.

Excerpt:

The Encoding API makes it simple to translate between raw bytes and native JavaScript strings, regardless of which of the many standard encodings you need to work with.

<pre id="results"></pre>

<script>
  if ('TextDecoder' in window) {
    // The local files to be fetched, mapped to the encoding that they're using.
    var filesToEncoding = {
      'utf8.bin': 'utf-8',
      'utf16le.bin': 'utf-16le',
      'macintosh.bin': 'macintosh'
    };

    Object.keys(filesToEncoding).forEach(function(file) {
      fetchAndDecode(file, filesToEncoding[file]);
    });
  } else {
    document.querySelector('#results').textContent = 'Your browser does not support the Encoding API.'
  }

  // Use XHR to fetch `file` and interpret its contents as being encoded with `encoding`.
  function fetchAndDecode(file, encoding) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', file);
    // Using 'arraybuffer' as the responseType ensures that the raw data is returned,
    // rather than letting XMLHttpRequest decode the data first.
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
      if (this.status == 200) {
        // The decode() method takes a DataView as a parameter, which is a wrapper on top of the ArrayBuffer.
        var dataView = new DataView(this.response);
        // The TextDecoder interface is documented at http://encoding.spec.whatwg.org/#interface-textdecoder
        var decoder = new TextDecoder(encoding);
        var decodedString = decoder.decode(dataView);
        // Add the decoded file's text to the <pre> element on the page.
        document.querySelector('#results').textContent += decodedString + '\n';
      } else {
        console.error('Error while requesting', file, this);
      }
    };
    xhr.send();
  }
</script>
🌐
npm
npmjs.com › package › arraybuffer-to-string
arraybuffer-to-string - npm
var ab2str = require('arraybuffer-to-string') var uint8 = new Uint8Array([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ab2str(uint8) // 'Hello World!' ab2str(uint8, 'base64') // 'SGVsbG8gV29ybGQh' ab2str(uint8, 'hex') // '48656c6c6f20576f726c6421' ab2str(uint8, 'iso-8859-2') // 'Hello World!'
      » npm install arraybuffer-to-string
    
Published   Feb 23, 2018
Version   1.0.2
🌐
GitHub
gist.github.com › skratchdot › e095036fad80597f1c1a
Array Buffer -> String and String -> ArrayBuffer conversions in javascript · GitHub
const byteArrayString = String.fromCharCode.apply(null, Array.from(new Uint8Array(anArrayBufferC))); The above code giving me "Maximum call stack size exceeded" error. Please help ... @erycson - nice! It looks like TextEncoder/TextDecoder is now a global in node (as of v11.0.0). It was added in the util lib in v8.3.0. Following your example. the ab2str function can be rewritten: function ab2str(buf) { return new TextDecoder().decode(buf); } ... Very good! Had to change the Uint16Array to Uint8Array and worked like a charm!
🌐
npm
npmjs.com › package › uint8array-extras
uint8array-extras - npm
August 22, 2025 - Convert a Base64-encoded or Base64URL-encoded string to a Uint8Array.
      » npm install uint8array-extras
    
Published   Aug 22, 2025
Version   1.5.0
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › toString
TypedArray.prototype.toString() - JavaScript - MDN Web Docs
July 10, 2025 - See Array.prototype.toString() for more details. This method is not generic and can only be called on typed array instances. ... const uint8 = new Uint8Array([1, 2, 3]); // Explicit conversion console.log(uint8.toString()); // 1,2,3 // Implicit conversion console.log(`${uint8}`); // 1,2,3
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-uint8array-from-method
JavaScript Uint8Array.from() Method | GeeksforGeeks
January 9, 2024 - Example 1: In this example, we will see the basic functionality of the Uint8Array array to create new Uint8array from a string of integers.