-
-
Save mkropat/b69d8102815daac9a4d198f6cb75252b to your computer and use it in GitHub Desktop.
Simple hexdump in Javascript
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 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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good modification. Base64 supporting. I have better: https://gist.github.com/username1565/18878422a72ef0e7f05edf72536b6ed9