-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.js
More file actions
36 lines (28 loc) · 990 Bytes
/
debug.js
File metadata and controls
36 lines (28 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const config = require('./config');
function hexDump(data, prefix = '') {
if (!config.DEBUG_PACKETS) return;
const width = 16;
let lines = [];
for (let i = 0; i < data.length; i += width) {
const chunk = data.slice(i, i + width);
// Hex part
const hex = [];
for (let j = 0; j < width; j++) {
if (j < chunk.length) {
hex.push(chunk[j].toString(16).padStart(2, '0').toUpperCase());
} else {
hex.push(' ');
}
}
// ASCII part
let ascii = '';
for (const byte of chunk) {
ascii += (byte >= 32 && byte <= 126) ? String.fromCharCode(byte) : '.';
}
lines.push(`${i.toString(16).padStart(4, '0')} ${hex.join(' ')} |${ascii}|`);
}
console.log(prefix);
console.log(lines.join('\n'));
console.log('-'.repeat(60));
}
module.exports = { hexDump };