Created
October 4, 2023 17:03
-
-
Save evsar3/3d800b5a56d75f140ba10deae7ed45d7 to your computer and use it in GitHub Desktop.
HexDump for TypeScript/Javascript
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 (data: Uint8Array, bytesPerLine = 16): string { | |
const result: string[] = [] | |
for (let i = 0; i < data.length; i += bytesPerLine) { | |
const chunk = data.slice(i, i + bytesPerLine) | |
const address = i.toString(16) | |
.padStart(8, '0') | |
const hex = Array.from(chunk, byte => byte.toString(16) | |
.padStart(2, '0')) | |
.join(' ') | |
.padEnd((bytesPerLine * 2) + (bytesPerLine - 1)) | |
const ascii = Array.from(chunk, byte => { | |
return (byte >= 32 && byte <= 126) | |
? String.fromCharCode(byte) | |
: '.' | |
}).join('') | |
.padEnd(bytesPerLine) | |
result.push(`${address}: ${hex} |${ascii}|`) | |
} | |
return result.join('\n') | |
} | |
/* Output Example | |
00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
00000080: 00 00 00 00 00 00 00 |....... | | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment