Created
March 25, 2020 05:09
-
-
Save phoddie/cb7e94965f9ecfbe111ede8ee281097c to your computer and use it in GitHub Desktop.
hex dump an ArrayBuffer
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 traceBuffer(buffer) { | |
const bytes = new Uint8Array(buffer); | |
trace(`${bytes.length} byte ArrayBuffer\n`); | |
for (let i = 0; i < bytes.length; i += 16) { | |
let use = Math.min(bytes.length - i, 16); | |
let line = ""; | |
for (let j = i; j < (i + use); j++) { | |
if (j !== i) | |
line += ", "; | |
line += bytes[j].toString(16).padStart(2, 0); | |
} | |
line += " "; | |
for (let j = i; j < (i + use); j++) { | |
let byte = bytes[j]; | |
if ((32 <= byte) && (byte <= 128)) | |
line += String.fromCharCode(byte); | |
else | |
line += "."; | |
} | |
trace(line, "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment