Created
March 15, 2016 16:42
-
-
Save igorgatis/d294fe714a4f523ac3a3 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 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
Example usage:
As you can see,
buffer
, this is notArrayBuffer
, juststring
.So if anyone want to use
ArrayBuffer
here, asbuffer
, you can use the following modification: