Created
July 7, 2023 07:48
-
-
Save leaysgur/212157c457ea34e9c99b59e262c4244e to your computer and use it in GitHub Desktop.
ArrayBuffer <-> HEX string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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