Skip to content

Instantly share code, notes, and snippets.

@igorgatis
Created March 15, 2016 16:42
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save igorgatis/d294fe714a4f523ac3a3 to your computer and use it in GitHub Desktop.
Save igorgatis/d294fe714a4f523ac3a3 to your computer and use it in GitHub Desktop.
Simple hexdump in Javascript
function hexdump(buffer, blockSize) {
blockSize = blockSize || 16;
var lines = [];
var hex = "0123456789ABCDEF";
for (var b = 0; b < buffer.length; b += blockSize) {
var block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
var addr = ("0000" + b.toString(16)).slice(-4);
var codes = block.split('').map(function (ch) {
var code = ch.charCodeAt(0);
return " " + hex[(0xF0 & code) >> 4] + hex[0x0F & code];
}).join("");
codes += " ".repeat(blockSize - block.length);
var chars = block.replace(/[\x00-\x1F\x20]/g, '.');
chars += " ".repeat(blockSize - block.length);
lines.push(addr + " " + codes + " " + chars);
}
return lines.join("\n");
}
@username1565
Copy link

username1565 commented Nov 28, 2019

Just created fork, here, including modification from @mkropat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment