Last updated: Mar 7, 2024
Reading timeยท3 min
To convert a string to a byte array in JavaScript:
TextEncoder()
constructor to create a TextEncoder
object.encode()
method on the object to convert the string to a byte
array.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);
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.
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.
You can create a TextDecoder object if you need to convert the Byte Array back to a string.
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
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.
// ๐๏ธ 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.
Buffer.from()
to convert a String to a Byte Array in JavaScriptIf you use Node.js, you can also use the Buffer.from method to convert a string to a byte array.
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
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.
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
.
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.
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.
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());
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.