Update 2018 - The easiest way in 2018 should be TextEncoder
let utf8Encode = new TextEncoder();
utf8Encode.encode("abc");
// Uint8Array [ 97, 98, 99 ]
Caveats - The returned element is a Uint8Array, and not all browsers support it.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-convert-string-to-bytes
JavaScript Program to Convert String to Bytes - GeeksforGeeks
July 23, 2025 - The Buffer class in Node.js can be used to convert a string into bytes by specifying the encoding type. This approach is particularly useful for server-side JavaScript running in a Node.js environment.
Top answer 1 of 12
92
Update 2018 - The easiest way in 2018 should be TextEncoder
let utf8Encode = new TextEncoder();
utf8Encode.encode("abc");
// Uint8Array [ 97, 98, 99 ]
Caveats - The returned element is a Uint8Array, and not all browsers support it.
2 of 12
41
If you are looking for a solution that works in node.js, you can use this:
var myBuffer = [];
var str = 'Stack Overflow';
var buffer = new Buffer(str, 'utf16le');
for (var i = 0; i < buffer.length; i++) {
myBuffer.push(buffer[i]);
}
console.log(myBuffer);
how to convert string to bytes in javascript - Stack Overflow
When creating a Uint8Array, the data is stored as bytes, which is exactly what you are looking for here. What you've done when using .join() is to create them as strings. More on stackoverflow.com
Bug converting string to bytes32
Maybe you're aware of this but I did some investigating in Remix to understand the issue. It seems memory is structured into 32 byte slots. Arrays consist of a 32 byte slot that represents it's size (length) in whatever the type specifies (eg, a string is a byte array, so the size is how many individual bytes the array is made up of, alternatively for a uint256[] array which consists of 32 byte slots, the size is how many 32 byte slots it takes up) followed by 32 byte chunks of the bytes of the data (as many 32 byte slots as is required to store the whole length of the array). This means that if you pass an empty string or pass nothing, the first 32 byte slot of the string (length) is 0 and the following 32 byte slot which would hold the first 32 bytes of the data has nothing to go in it so will just be used for the next thing stored in memory (which is what will be loaded by the assembly code). There's no point reserving an empty 32 byte slot for no data. More on reddit.com
Is there a way to encode raw bytes in Shift-JIS?
It seems like the array data is a double-encoded text. e.g. Shift-JIS then GB2312, or GB2312 then Shift-JIS. My guess is Shift-JIS then GB2312. i.e. originally a Shift-JIS encoded text, then wrapped with GB2312 encoding. Note: GB2312 character set also includes Japanese katakana characters. More on reddit.com
Confused how to convert a string to a byte array. Need to convert equivalent C# snippet in JS
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Appendix.3A_Decode_a_Base64_string_to_Uint8Array_or_ArrayBuffer
More on reddit.comHow do I run JavaScript locally vs in the browser?
In the browser: open DevTools (F12), Console tab, paste code, Enter. For HTML pages, add a script tag pointing to your .js file. Locally: install Node.js (LTS), then node script.js in your terminal from the file folder.
itsourcecode.com
itsourcecode.com › home › how to convert string to byte array in javascript?
How to Convert String to Byte Array in JavaScript (2026)
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds top-level await, private class fields with #, and the .at() array method. For older browsers, transpile with Babel or use Vite, esbuild, or webpack.
itsourcecode.com
itsourcecode.com › home › how to convert string to byte array in javascript?
How to Convert String to Byte Array in JavaScript (2026)
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard with IntelliSense, debugger, terminal, Git, and a huge extension marketplace. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
itsourcecode.com
itsourcecode.com › home › how to convert string to byte array in javascript?
How to Convert String to Byte Array in JavaScript (2026)
Reddit
reddit.com › r/javascripttips › how to convert string to byte array in javascript?
r/JavaScriptTips on Reddit: How to Convert String to Byte Array in JavaScript?
May 1, 2023 - This object provides a view of an underlying ArrayBuffer object, which represents a fixed-length, contiguous block of memory. To convert a JavaScript string to a byte array, we can use the TextEncoder interface.
DEV Community
dev.to › rajnishkatharotiya › get-byte-size-of-the-string-in-javascript-20jm
Get Byte size of the string in Javascript - DEV Community
April 12, 2020 - In this series, I’m going to talk about some shortcodes and useful functions of javascript. These snippets can help you to make your development more efficient and faster. Stay tuned till the end to learn something new… 😊 · As we all know a byte is one the unit of digital information, and while development taking care of the size of variables, records and files are a much important task. To do that we have various ways but with these functions, it’s very easy to do. byteSize() snippet will take a string as an input and in the output, it’ll return byte size of a given string.
Itsourcecode
itsourcecode.com › home › how to convert string to byte array in javascript?
How to Convert String to Byte Array in JavaScript (2026)
July 9, 2026 - We then delved into the step-by-step process of converting a string into a byte array using the TextEncoder object in JavaScript. We created an instance of TextEncoder, defined our string, used the encode method to convert the string into bytes, and finally logged the byte array to the console.
GitHub
gist.github.com › robingenz › 5736ffd3d2f23966eabec721bb03133e
String to byte array converter in JavaScript · GitHub
String to byte array converter in JavaScript. GitHub Gist: instantly share code, notes, and snippets.
Stack Overflow
stackoverflow.com › questions › 45137229 › how-to-convert-string-to-bytes-in-javascript
how to convert string to bytes in javascript - Stack Overflow
July 17, 2017 - Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... const file = e.files[0]; var reader = new FileReader(); reader.readAsArrayBuffer(file.rawFile); reader.onload = function () { var strBytes = new Uint8Array(reader.result).join(); var byte=???; }
GitHub
gist.github.com › raphaelchaib › 0eb1bebd5142eab4b099159d3d35d22a
Javascript: String to Bytes array · GitHub
August 16, 2016 - Javascript: String to Bytes array. GitHub Gist: instantly share code, notes, and snippets.
GeeksforGeeks
geeksforgeeks.org › how-to-get-the-length-of-a-string-in-bytes-in-javascript
How to Get the Length of a String in Bytes in JavaScript ? - GeeksforGeeks
June 24, 2024 - The TextEncoder API provides an efficient way to encode a string into a specific format (e.g., UTF-8) and retrieve its byte length.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromHex
Uint8Array.fromHex() - JavaScript - MDN Web Docs
July 10, 2025 - This example decodes a hexadecimal string into a Uint8Array. ... const hexString = "cafed00d"; const bytes = Uint8Array.fromHex(hexString); console.log(bytes); // Uint8Array [ 202, 254, 208, 13 ]