I\'ve got a Javascript ArrayBuffer that I would like to be converted into a hex string.
Anyone knows of a function that I can call or a pre written function already
Here is a sweet ES6 solution, using padStart and avoiding the quite confusing prototype-call-based solution of the accepted answer. It is actually faster as well.
function bufferToHex (buffer) {
return [...new Uint8Array (buffer)]
.map (b => b.toString (16).padStart (2, "0"))
.join ("");
}
How this works:
Array is created from a Uint8Array holding the buffer data. This is so we can modify the array to hold string values later.Array items are mapped to their hex codes and padded with 0 characters.