hexdump.js/hexdump.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-09-22 23:07:07 +00:00
(function (exports) {
'use strict';
2017-10-07 02:30:21 +00:00
exports.hexdump = function hexdump(ab, offset, len, opts) {
if (!opts) { opts = {}; }
2017-09-25 18:00:04 +00:00
var ui8 = new Uint8Array(ab.buffer || ab, offset || ab.byteOffset, len || ab.byteLength);
2017-09-22 23:07:07 +00:00
var bytecount = 0;
var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F';
var trail;
var str = [].slice.call(ui8).map(function (i) {
var h = i.toString(16);
if (h.length < 2) {
h = '0' + h;
}
return h;
}).join('').match(/.{1,2}/g).join(' ').match(/.{1,48}/g).map(function (str) {
var lead = bytecount.toString(16);
bytecount += 16;
while (lead.length < 7) {
lead = '0' + lead;
}
2017-10-07 02:30:21 +00:00
while (str.length < 48) {
str += ' ';
}
if (opts.C) {
return lead + ' ' + str + ' |' + str.replace(/ /g, '').match(/.{1,2}/g).map(function (ch) {
var c = String.fromCharCode(parseInt(ch, 16));
if (!/[ -~]/.test(c)) {
c = '.';
}
return c;
}).join('') + '|';
} else {
return lead + ' ' + str;
}
2017-09-22 23:07:07 +00:00
}).join('\n');
2017-09-25 18:00:04 +00:00
trail = (len || ab.byteLength).toString(16);
2017-09-22 23:07:07 +00:00
while (trail.length < 7) {
trail = '0' + trail;
}
return head + '\n' + str + '\n' + trail;
};
}('undefined' !== typeof exports && exports || 'undefined' !== typeof window && window || global));