Skip to content

Instantly share code, notes, and snippets.

@phoddie
Last active March 25, 2020 05:16
Show Gist options
  • Save phoddie/85f1344590e73a935eed5cd374f4c108 to your computer and use it in GitHub Desktop.
Save phoddie/85f1344590e73a935eed5cd374f4c108 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 line = [];
const end = i + Math.min(bytes.length - i, 16);
for (let j = i; j < end; j++)
line.push(bytes[j].toString(16).padStart(2, 0));
line = line.join(", ") + " ";
for (let j = i; j < end; j++) {
const 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