75 lines
1.3 KiB
JavaScript
75 lines
1.3 KiB
JavaScript
'use strict'
|
|
|
|
const wout = (s) => {
|
|
if (typeof process !== 'undefined' && process.stdout) {
|
|
process.stdout.write(s)
|
|
} else if (typeof console !== 'undefined') {
|
|
if (s === '\n') {
|
|
console.log('')
|
|
} else {
|
|
process.stdout = process.stdout || ''
|
|
process.stdout += s
|
|
}
|
|
}
|
|
}
|
|
|
|
const padStr = (s, len, ch) => {
|
|
ch = ch || ' '
|
|
while (s.length < len) {
|
|
s = ch + s
|
|
}
|
|
return s
|
|
}
|
|
|
|
const hexDigit = (n, len) => {
|
|
return padStr(n.toString(16), len || 2, '0')
|
|
}
|
|
|
|
const printableCh = (c) => {
|
|
// [space to '~')
|
|
if (c > 0x20 && c <= 0x7e) {
|
|
return String.fromCharCode(c)
|
|
} else {
|
|
return '.'
|
|
}
|
|
}
|
|
|
|
const hexdump = (buf, len) => {
|
|
len = len || buf.length
|
|
|
|
const maxline = 16
|
|
let ascii = new Array(maxline)
|
|
let i
|
|
|
|
for (i = 0; i < len; i++) {
|
|
if (i % maxline === 0) {
|
|
if (i > 0) {
|
|
wout(' ' + ascii.join(''))
|
|
wout('\n')
|
|
}
|
|
wout(hexDigit(i, 4) + ': ')
|
|
}
|
|
|
|
// output the hex digit
|
|
wout(hexDigit(buf[i]))
|
|
wout(' ')
|
|
ascii[i % maxline] = printableCh(buf[i])
|
|
}
|
|
if (i % maxline !== 0) {
|
|
let diff = maxline - (i % maxline)
|
|
wout(' '.repeat(diff))
|
|
}
|
|
|
|
wout(' ' + ascii.slice(0, (i % maxline)).join('') + '\n')
|
|
}
|
|
|
|
/*
|
|
let buf = new Uint8Array(500)
|
|
for (let i = 0; i < buf.length; i++) {
|
|
buf[i] = i & 0xff
|
|
}
|
|
hexdump(buf)
|
|
*/
|
|
|
|
module.exports = hexdump
|