Skip to content

Instantly share code, notes, and snippets.

@mkropat
Forked from igorgatis/hexdump.js
Last active November 28, 2019 08:27
Show Gist options
  • Save mkropat/b69d8102815daac9a4d198f6cb75252b to your computer and use it in GitHub Desktop.
Save mkropat/b69d8102815daac9a4d198f6cb75252b to your computer and use it in GitHub Desktop.
Simple hexdump in Javascript
function d(str) {
console.log(hexdump(decode(str)));
}
function decode(base64) {
base64 = base64 || '';
return atob(base64.replace(/_/g, '/').replace(/-/g, '+'));
}
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

Good modification. Base64 supporting. I have better: https://gist.github.com/username1565/18878422a72ef0e7f05edf72536b6ed9

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