Created
July 22, 2022 13:15
-
-
Save aib/6317ad3e9aa6e89b984ccac060f924f8 to your computer and use it in GitHub Desktop.
JavaScript hexdump
This file contains hidden or 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, bytesPerRow) { | |
bytesPerRow = bytesPerRow || 16; | |
const hexDigits = "01234567890abcdef"; | |
var str = ""; | |
for (var addr = 0; addr < buffer.length; addr += bytesPerRow) { | |
var rowStr = ("0000" + addr.toString(16)).slice(-4) + ":"; | |
for (var i = addr; i < addr + bytesPerRow; i++) { | |
if (i < buffer.length) { | |
rowStr += " " + hexDigits[(0xf0 & buffer[i]) >> 4] + hexDigits[0x0f & buffer[i]]; | |
} else { | |
rowStr += " "; | |
} | |
} | |
rowStr += " "; | |
for (var i = addr; i < addr + bytesPerRow; i++) { | |
if (i < buffer.length) { | |
if (buffer[i] > 32 && buffer[i] < 127) { | |
rowStr += String.fromCharCode(buffer[i]); | |
} else { | |
rowStr += "."; | |
} | |
} | |
} | |
str += rowStr + "\n"; | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment