Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active September 13, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/dde9dd8571341a63f9f0aa4652e8f00a to your computer and use it in GitHub Desktop.
Save JosePedroDias/dde9dd8571341a63f9f0aa4652e8f00a to your computer and use it in GitHub Desktop.
render binary content as hex editor do

Renders binary content such as Uint8Arrays to an hex layout similar to hex editor such as Hex Fiend and Frhed

Example output:

0000 0000001c 66747970 69736f36 00000001 69736f6d 69736f36 6d736468 000002a7 
0020 6d6f6f76 00000078 6d766864 01000000 00000000 00000000 00000000 00000000 
0040 00989680 00000000 53291000 00010000 01000000 00000000 00000000 00010000 
0060 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 
0080 00000000 00000000 00000000 00000000 00000000 00000000 00000002 000001ff 
00a0 7472616b 00000068 746b6864 01000007 00000000 00000000 00000000 00000000 
00c0 00000001 00000000 00000000 53291000 00000000 00000000 00000000 01000000 
00e0 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 
0100 40000000 01c00000 00fc0000 0000018f 6d646961 0000002c 6d646864 01000000 
0120 00000000 00000000 00000000 00000000 00989680 00000000 53291000 55c40000 
0140 0000002c 68646c72 00000000 00000000 76696465 00000000 00000000 00000000 
0160 56696465 6f205472 61636b00 0000012f 6d696e66 00000014 766d6864 00000001 
0180 00000000 00000000 00000024 64696e66 0000001c 64726566 00000000 00000001 
01a0 0000000c 75726c20 00000001 000000ef 7374626c 00000010 73747473 00000000 
01c0 00000000 00000010 73747363 00000000 00000000 00000010 7374636f 00000000 
01e0 00000000 00000014 7374737a 00000000 00000000 00000000 000000a3 73747364 
0200 00000000 00000001 00000093 61766331 00000000 00000001 00000000 00000000 
0220 00000000 00000000 01c000fc 00480000 00480000 00000000 00010a41 56432043 
0240 6f64696e 67000000 00000000 00000000 00000000 00000000 00000018 ffff0000 
0260 003d6176 6343014d 401effe1 0025674d 401e9652 83810fde 02d10000 03000100 
0280 00030032 e0000124 f000249f 7f18e0ed 0c168b01 000568e9 08352000 0000286d 
02a0 76657800 00002074 72657800 00000000 00000100 00000100 00000000 00000000 
02c0 000000
function hex(n) {
return ('0' + n.toString(16)).slice(-2);
}
function hex2(n) {
return hex(~~(n / 256)) + hex(n % 256);
}
function hexview(arr) {
return Array.from(arr).map(function(c, i) {
return (i % 4 === 0 ? ' ' : '') + (i % 32 === 0 ? '\n' + hex2(i) + ' ' : '') + hex(c);
}).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment