Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created July 7, 2023 07:48
Show Gist options
  • Save leaysgur/212157c457ea34e9c99b59e262c4244e to your computer and use it in GitHub Desktop.
Save leaysgur/212157c457ea34e9c99b59e262c4244e to your computer and use it in GitHub Desktop.
ArrayBuffer <-> HEX string
function arrayBufferToHex(arrayBuffer) {
const view = new Uint8Array(arrayBuffer);
let result = "";
for (let i = 0; i < view.length; i++) {
const value = view[i].toString(16);
result += value.length === 1 ? "0" + value : value;
}
return result;
}
function hexToArrayBuffer(hex) {
const view = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2)
view[i / 2] = parseInt(hex.substring(i, i + 2), 16);
return view.buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment