Converting String to Uint8Array & Vice-Versa in Javascript
Here are the ways to convert a string to Uint8Array and vice-versa:
Converting String to Uint8Array
1. Using TextEncoder
const str = 'Hello, World!';
const encoder = new TextEncoder();
const uint8Array = encoder.encode(str);
2. Using charCodeAt()
and Uint8Array
constructor
const str = 'Hello, World!';
const uint8Array = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
uint8Array[i] = str.charCodeAt(i);
}
3. Using charCodeAt()
and Array.prototype.map()
const str = 'Hello, World!';
const uint8Array = Array.prototype.map.call(str, (c) => c.charCodeAt(0));
4. Using Buffer
(Node.js only)
const str = 'Hello, World!';
const uint8Array = Buffer.from(str, 'utf-8');
5. Using ArrayBuffer
and DataView
const str = 'Hello, World!';
const arrayBuffer = new ArrayBuffer(str.length);
const dataView = new DataView(arrayBuffer);
for (let i = 0; i < str.length; i++) {
dataView.setUint8(i, str.charCodeAt(i));
}
const uint8Array = new Uint8Array(arrayBuffer);
6. Using charCodeAt
and for loop
// Convert string to Uint8Array manually
function stringToUint8Array(str) {
const uint8Array = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
uint8Array[i] = str.charCodeAt(i);
}
return uint8Array;
}
const str = "Hello, World!";
const uint8ArrayManual = stringToUint8Array(str);
console.log(uint8ArrayManual); // Output: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
7. Using split()
and map()
const str = 'Hello, World!';
const uint8Array = str.split('').map((c) => c.charCodeAt(0));
Converting Uint8Array to String
1. Using TextDecoder
const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const decoder = new TextDecoder();
const str = decoder.decode(uint8Array);
2. Using String.fromCharCode()
and Array.prototype.map()
const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const str = Array.prototype.map.call(uint8Array, (c) => String.fromCharCode(c)).join('');
3. Using Buffer
(Node.js only)
const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const str = Buffer.from(uint8Array).toString('utf-8');
4. Using DataView
and ArrayBuffer
const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const arrayBuffer = uint8Array.buffer;
const dataView = new DataView(arrayBuffer);
const str = '';
for (let i = 0; i < uint8Array.length; i++) {
str += String.fromCharCode(dataView.getUint8(i));
}
5. Using Array.prototype.reduce()
const uint8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const str = uint8Array.reduce((acc, c) => acc + String.fromCharCode(c), '');
Latest blog posts
Explore the world of programming and cybersecurity through our curated collection of blog posts. From cutting-edge coding trends to the latest cyber threats and defense strategies, we've got you covered.