Skip to content

Instantly share code, notes, and snippets.

@phoddie
Created March 25, 2020 05:09
Show Gist options
  • Save phoddie/cb7e94965f9ecfbe111ede8ee281097c to your computer and use it in GitHub Desktop.
Save phoddie/cb7e94965f9ecfbe111ede8ee281097c to your computer and use it in GitHub Desktop.
hex dump an ArrayBuffer
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