Skip to main content

How to Convert a String to a Byte Array in JavaScript

Converting a string into a "byte array" is a common requirement when working with binary data, cryptography, or low-level data protocols. A byte array is essentially a sequence of numbers (integers from 0-255) that represent the string's characters in a specific encoding, most commonly UTF-8.

This guide will teach you the modern, standard method for this conversion using the TextEncoder API, which works in both browsers and Node.js. We will also cover the reverse operation (byte array to string) and the Node.js-specific Buffer API.

The Core Concept: Text Encoding

A string like "A" is not a number. To represent it in binary, it must be encoded. The UTF-8 encoding standard assigns the character "A" the numerical value 65. A "byte array" is an array of these numerical values. The TextEncoder API in JavaScript is the standard tool for performing this UTF-8 encoding.

The Modern Method (Universal): TextEncoder

The TextEncoder interface is a standard Web API that is available in all modern browsers and in Node.js. It is the recommended best practice for converting strings to UTF-8 bytes.

The logic:

  1. Create an instance of TextEncoder.
  2. Call the .encode() method on the instance, passing it your string.
  3. The method returns a Uint8Array, which is a special type of array that holds 8-bit unsigned integers (bytes).

For example, you have a string and you want to get its UTF-8 byte representation.

// Problem: How to convert this string into a sequence of bytes?
const myString = 'Hello!';

Solution:

// 1. Create an encoder instance.
const encoder = new TextEncoder();

const myString = 'Hello!';

// 2. Encode the string.
const byteArray = encoder.encode(myString);

console.log(byteArray);

Output:

Uint8Array(6) [ 72, 101, 108, 108, 111, 33 ]

Note that: 72 is 'H', 101 is 'e', etc.

note

This is the most direct and universally compatible method.

The Node.js-Specific Method: Buffer.from()

If you are working exclusively in a Node.js environment, the built-in Buffer class provides a more direct way to handle binary data.

The solution is to use the Buffer.from(string, [encoding]) method that creates a new Buffer containing the bytes of the given string.

// This code only works in Node.js
const myString = 'Hello!';

// Create a Buffer from the string (defaults to 'utf8' encoding)
const buffer = Buffer.from(myString);

console.log(buffer);
// Output: <Buffer 48 65 6c 6c 6f 21>
// (This is the hexadecimal representation of the same byte values)

// You can see the decimal values by iterating
console.log(Array.from(buffer));
// Output: [ 72, 101, 108, 108, 111, 33 ]
note

While Buffer is very powerful in Node.js, TextEncoder is the more universal standard, making your code more portable to browser environments.

Bonus: Converting a Byte Array Back to a String

The reverse operation is also simple and uses the TextDecoder API, which is the counterpart to TextEncoder.

Universal Solution

const byteArray = new Uint8Array([72, 101, 108, 108, 111, 33]);

// 1. Create a decoder instance.
const decoder = new TextDecoder();

// 2. Decode the byte array back into a string.
const originalString = decoder.decode(byteArray);

console.log(originalString);
// Output: "Hello!"

Solution for Node.js using Buffer

warning

This code only works in Node.js

// This code only works in Node.js
const buffer = Buffer.from([72, 101, 108, 108, 111, 33]);

const originalString = buffer.toString('utf8');

console.log(originalString);
// Output: "Hello!"

Conclusion

For converting a string to a byte array in JavaScript, the method you choose depends on your target environment.

  • The TextEncoder API is the recommended best practice. It is a modern, web-standard API that works in both browsers and Node.js, making your code universally compatible.
  • The Buffer.from() method is a powerful and direct alternative if you are working exclusively in a Node.js environment.

For most new projects, standardizing on TextEncoder and TextDecoder provides the most portable and future-proof solution.