Naegsh Pachorkar
Naegsh Pachorkar

Reputation: 49

Base 16 encoding of Uint8Array.buffer data

I am using javascript Uint8Array object. I get a binary data out of it using '.buffer' property.

Due to some reasons I want to encode the binary data to base16 but I haven't got the clue yet. Any help in the regard would be helpful.

Upvotes: 1

Views: 1245

Answers (2)

Esailija
Esailija

Reputation: 140220

var a = new Uint8Array(4);

function pad(str ) {
    return str.length < 2 ? "0" + str : str;
}

[].map.call( a, function(v) {
    return pad(v.toString(16));
}).join(""); //00000000

Upvotes: 1

lostsource
lostsource

Reputation: 21830

Not sure if this is what you need

var b16 = "";
var u8 = [8,9,10,11]; // sample input

for(var x = 0; x < u8.length; x++) {
    b16 += pad2(u8[x].toString(16));
}

function pad2(str) {
    return (str.length < 2) ? "0"+str : str;
}

console.log(b16); // will return string "08090a0b"

Upvotes: 1

Related Questions