Last active
January 28, 2024 15:25
-
-
Save Vbitz/ea301a2797e73616af8048d80f9d1aef to your computer and use it in GitHub Desktop.
Simple node.js hexdump function written in TypeScript.
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 printValue(value: number): string { | |
if (value >= 0x20 && value <= 0x7e) { | |
return String.fromCharCode(value); | |
} else { | |
return '.'; | |
} | |
} | |
export function hexDump(buff: Buffer): string { | |
let x = 0; | |
let lineHex = ''; | |
let lineAscii = ''; | |
let ret = ''; | |
let offset = 0; | |
for (const value of buff) { | |
lineHex += value.toString(16).padStart(2, '0') + ' '; | |
lineAscii += printValue(value); | |
x += 1; | |
if (x > 16) { | |
ret += `${offset | |
.toString(16) | |
.padStart(8, '0')} | ${lineHex.trim()} | ${lineAscii}\n`; | |
lineHex = ''; | |
lineAscii = ''; | |
x = 0; | |
offset += 16; | |
} | |
} | |
ret += `${offset.toString(16).padStart(8, '0')} | ${lineHex | |
.trim() | |
.padEnd(3 * 16 + 2, ' ')} | ${lineAscii}\n`; | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment