How to convert string to byte array in JavaScript

1 Answer

0 votes
const str = "JavaScript";

// Convert string to byte array
const encoder = new TextEncoder();
const byteArray = encoder.encode(str);

console.log(byteArray);

const byteArray_length = byteArray.length;
for (let i = 0; i < byteArray_length; i++) {
    console.log(byteArray[i]);
}

 
 
/*
run:
 
Uint8Array(10) [
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
74
97
118
97
83
99
114
105
112
116
 
*/

 



answered Mar 10, 2025 by avibootz
...