Skip to content

String to Uint8Array & Uint8Array to String in JavaScript (Fast)

Convert strings to Uint8Array and back using TextEncoder, TextDecoder, and Buffer. Handles UTF-8 Unicode characters safely with benchmarked examples.

Tested On: Node.js 18+, Bun, Deno, and all modern browsers (Chrome, Edge, Firefox, Safari).

Use new TextEncoder().encode(text) to convert a JavaScript string to a UTF-8 Uint8Array. Convert it back with new TextDecoder().decode(bytes). These APIs are Unicode-safe and work in modern browsers and Node.js.

Two-Way Quick Reference (AEO / TL;DR)

// 1. String to Uint8Array (UTF-8)
const bytes = new TextEncoder().encode('Hello, World! 🚀');

// 2. Uint8Array to String (UTF-8)
const text = new TextDecoder().decode(bytes);

Working with binary data for cryptography or files? See our guide on JavaScript TypedArrays (Uint8, Uint16, Uint32) and AES encryption with Web Crypto.


Converting String to Uint8Array

The TextEncoder API is the most straightforward and efficient way to convert a string to a Uint8Array. It handles encoding automatically.

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]

Pros:

  • Simple and concise.
  • Handles UTF-8 encoding by default.

Resources:


2. Using charCodeAt() and Uint8Array Constructor

If you need more control or are working in an environment without TextEncoder, you can manually convert each character to its Unicode value.

const str = 'Hello, World!';
const uint8Array = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
  uint8Array[i] = str.charCodeAt(i);
}
console.log(uint8Array); // Output: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

Pros:

  • Works in all environments.
  • No external APIs required.

3. Using charCodeAt() and Array.prototype.map()

This method uses functional programming to achieve the same result as above.

const str = 'Hello, World!';
const uint8Array = new Uint8Array([...str].map((c) => c.charCodeAt(0)));
console.log(uint8Array); // Output: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

Pros:

  • Concise and readable.
  • Leverages JavaScript’s functional programming features.

4. Using Buffer (Node.js Only)

In Node.js, the Buffer class provides a simple way to handle binary data.

const str = 'Hello, World!';
const uint8Array = Buffer.from(str, 'utf-8');
console.log(uint8Array); // Output: <Buffer 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21>

Pros:

  • Optimized for Node.js environments.
  • Supports multiple encodings.

Resources:


5. Using ArrayBuffer and DataView

For advanced use cases, you can use ArrayBuffer and DataView to manually manipulate binary data.

const str = 'Hello, World!';
const arrayBuffer = new ArrayBuffer(str.length);
const dataView = new DataView(arrayBuffer);
for (let i = 0; i < str.length; i++) {
  dataView.setUint8(i, str.charCodeAt(i));
}
const uint8Array = new Uint8Array(arrayBuffer);
console.log(uint8Array); // Output: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

Pros:

  • Provides fine-grained control over binary data.
  • Useful for low-level operations.

Converting Uint8Array to String

The TextDecoder API is the easiest way to convert a Uint8Array back to a string.

const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const decoder = new TextDecoder();
const str = decoder.decode(uint8Array);
console.log(str); // Output: "Hello, World!"

Pros:

  • Simple and efficient.
  • Handles UTF-8 decoding by default.

Resources:


2. Using String.fromCharCode() and Array.prototype.map()

This method manually converts each byte to a character.

const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const str = Array.from(uint8Array, (c) => String.fromCharCode(c)).join('');
console.log(str); // Output: "Hello, World!"

Pros:

  • Works in all environments.
  • No external APIs required.

3. Using Buffer (Node.js Only)

In Node.js, you can use Buffer to convert a Uint8Array to a string.

const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const str = Buffer.from(uint8Array).toString('utf-8');
console.log(str); // Output: "Hello, World!"

Pros:

  • Optimized for Node.js environments.
  • Supports multiple encodings.

4. Using Array.prototype.reduce()

This method uses reduce to concatenate characters into a string.

const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const str = uint8Array.reduce((acc, c) => acc + String.fromCharCode(c), '');
console.log(str); // Output: "Hello, World!"

Pros:

  • Functional programming approach.
  • Concise and readable.

Best Practices

  1. Use TextEncoder and TextDecoder for Modern Applications: These APIs are efficient, standardized, and handle UTF-8 encoding/decoding seamlessly.
  2. Fallback for Older Environments: If you need to support older browsers or environments, use manual methods like charCodeAt() and String.fromCharCode().
  3. Node.js Optimization: In Node.js, prefer Buffer for its performance and ease of use.

Conclusion

Converting between strings and Uint8Array is a common task in JavaScript, especially when dealing with binary data or text encoding. By using the methods outlined in this guide, you can handle these conversions efficiently and effectively. Whether you’re working in a browser or Node.js environment, there’s a solution for every use case.

For further reading, check out these resources:

Happy coding! 🚀

Topics & Technologies

JavaScript string to Uint8ArrayUint8Array to stringTextEncoderTextDecoderNode.js Buffer

Continue Reading

View all →