From af6f9b37ebb17b311fb954a63daa1aa7ee8fd8fc Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 22 Sep 2017 17:07:07 -0600 Subject: [PATCH] add hexdump.js --- hexdump.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 hexdump.js diff --git a/hexdump.js b/hexdump.js new file mode 100644 index 0000000..34e2486 --- /dev/null +++ b/hexdump.js @@ -0,0 +1,32 @@ +(function (exports) { +'use strict'; + +exports.hexdump = function hexdump(ab) { + var ui8 = new Uint8Array(ab); + 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; + } + + return lead + ' ' + str; + }).join('\n'); + trail = ab.byteLength.toString(16); + while (trail.length < 7) { + trail = '0' + trail; + } + return head + '\n' + str + '\n' + trail; +}; + +}('undefined' !== typeof exports && exports || 'undefined' !== typeof window && window || global));