Skip to content

Instantly share code, notes, and snippets.

@detunized
Created September 1, 2016 08:44
Show Gist options
  • Save detunized/29f8ae22cc883d2cd072962e1db5a2eb to your computer and use it in GitHub Desktop.
Save detunized/29f8ae22cc883d2cd072962e1db5a2eb to your computer and use it in GitHub Desktop.
Hex dump for Node
function printHexDump(s) {
const W = 16
let bytes = Buffer.from(s)
for (let i = 0; i < bytes.length;) {
let hexRow = []
let asciiRow = []
let rowOffset = i
for (let x = 0; x < W; ++x, ++i) {
if (i < bytes.length) {
let c = bytes[i]
hexRow.push(("0" + (c).toString(16)).slice(-2))
asciiRow.push(c < 32 ? "." : String.fromCharCode(c))
} else {
hexRow.push(" ")
asciiRow.push(" ")
}
}
let row = [
("000" + rowOffset.toString(16)).slice(-4),
" | ",
hexRow.join(" "),
" | ",
asciiRow.join("")
]
console.log(row.join(" "))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment