How to convert a String to a Byte Array in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
3 min

banner

# How to convert a String to a Byte Array in JavaScript

To convert a string to a byte array in JavaScript:

  1. Instantiate the TextEncoder() constructor to create a TextEncoder object.
  2. Call the encode() method on the object to convert the string to a byte array.
index.js
const utf8EncodeText = new TextEncoder(); const str = 'bobbyhadz.com'; const byteArray = utf8EncodeText.encode(str); // Uint8Array(13) [ // 98, 111, 98, 98, 121, // 104, 97, 100, 122, 46, // 99, 111, 109 // ] console.log(byteArray);

convert string to bytearray

The code for this article is available on GitHub

This approach works in the browser and Node.js.

The TextEncoder() constructor creates a TextEncoder object that is used to generate a byte stream with UTF-8 encoding.

The last step is to use the TextEncoder.encode() method.

The method takes a string as a parameter and returns Uint8Array object.

A Uint8Array represents an array of 8-bit unsigned integers.

If you have to convert a string to a byte array often, define a reusable function.

index.js
function strToByteArray(str) { return new TextEncoder().encode(str); } const byteArray1 = strToByteArray('bobbyhadz.com'); // Uint8Array(13) [ // 98, 111, 98, 98, 121, // 104, 97, 100, 122, 46, // 99, 111, 109 // ] console.log(byteArray1);

The function takes a string as a parameter, converts the string to a byte array and returns the result.

# Decoding the Byte Array back to a string

You can create a TextDecoder object if you need to convert the Byte Array back to a string.

index.js
const utf8EncodeText = new TextEncoder(); const str = 'bobbyhadz.com'; const byteArray = utf8EncodeText.encode(str); // Uint8Array(13) [ // 98, 111, 98, 98, 121, // 104, 97, 100, 122, 46, // 99, 111, 109 // ] console.log(byteArray); const strAgain = new TextDecoder().decode(byteArray); console.log(strAgain); // ๐Ÿ‘‰๏ธ bobbyhadz.com

decode byte array to string

The code for this article is available on GitHub

We used the TextDecoder() constructor to create a TextDecoder object and called the decode method on the object.

The method takes a buffer (an ArrayBuffer, a TypedArray or a DataView object) and returns a string that contains the text decoded from the buffer.

If you have to decode a byte array often, create a reusable function.

index.js
// ๐Ÿ‘‡๏ธ converts a string to a byte array function strToByteArray(str) { return new TextEncoder().encode(str); } // ๐Ÿ‘‡๏ธ converts a byte array to a string function byteArrayToStr(buf) { return new TextDecoder().decode(buf); } const str = 'bobbyhadz.com'; const byteArray = strToByteArray(str); // Uint8Array(13) [ // 98, 111, 98, 98, 121, // 104, 97, 100, 122, 46, // 99, 111, 109 // ] console.log(byteArray); const strAgain = byteArrayToStr(byteArray); console.log(strAgain); // ๐Ÿ‘‰๏ธ bobbyhadz.com

The byteArrayToStr function takes a byte array as a parameter and converts the byte array to a string.

# Using Buffer.from() to convert a String to a Byte Array in JavaScript

If you use Node.js, you can also use the Buffer.from method to convert a string to a byte array.

index.js
const str = 'bobbyhadz.com'; const byteArray = Buffer.from(str); // ๐Ÿ‘‡๏ธ <Buffer 62 6f 62 62 79 68 61 64 7a 2e 63 6f 6d> console.log(byteArray); console.log(byteArray.toString()); // ๐Ÿ‘‰๏ธ bobbyhadz.com

convert string to byte array using buffer from

The code for this article is available on GitHub

The method creates a new Buffer that contains the given string.

Note that this approach cannot be used in the browser.

The Buffer.from() method takes a string and an encoding as parameters.

The encoding parameter is optional and defaults to utf8.

Here is an example that explicitly passes the encoding argument.

index.js
const str = 'bobbyhadz.com'; const byteArray = Buffer.from(str, 'utf8'); // ๐Ÿ‘‡๏ธ <Buffer 62 6f 62 62 79 68 61 64 7a 2e 63 6f 6d> console.log(byteArray);

The encoding parameter sets the character encoding that is used to convert the string to bytes.

If you need to convert the string to a UTF-16 Byte Array, set the encoding to utf16le.

index.js
const str = 'bobbyhadz.com'; const byteArray = Buffer.from(str, 'utf16le'); // ๐Ÿ‘‡๏ธ <Buffer 62 00 6f 00 62 00 62 00 79 00 68 00 61 00 64 00 7a 00 2e 00 63 00 6f 00 6d 00> console.log(byteArray);

You can use the toString() method if you need to decode the buffer into a string.

index.js
const str = 'bobbyhadz.com'; const byteArray = Buffer.from(str, 'utf16le'); // ๐Ÿ‘‡๏ธ <Buffer 62 00 6f 00 62 00 62 00 79 00 68 00 61 00 64 00 7a 00 2e 00 63 00 6f 00 6d 00> console.log(byteArray); console.log(byteArray.toString()); // ๐Ÿ‘‰๏ธ bobbyhadz.com

You can also define a reusable function if you have to convert a string to a byte array often.

index.js
function strToBytesArray(str, encoding = 'utf8') { return Buffer.from(str, encoding); } const str = 'bobbyhadz.com'; const byteArray = strToBytesArray(str, 'utf8'); // ๐Ÿ‘‡๏ธ <Buffer 62 6f 62 62 79 68 61 64 7a 2e 63 6f 6d> console.log(byteArray); // bobbyhadz.com console.log(byteArray.toString());

convert string to byte array using reusable function

The code for this article is available on GitHub

The function takes a string and optionally an encoding (defaults to utf8) as parameters and converts the string to a buffer.

I've also written an article on how to convert a JSON object to a Buffer and vice versa in Node.js.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev