Last active
March 25, 2020 05:16
-
-
Save phoddie/85f1344590e73a935eed5cd374f4c108 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 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