From 150e66fc99d9e14aeac82af261beac07c051fd15 Mon Sep 17 00:00:00 2001 From: j83305 Date: Sun, 31 May 2020 15:52:32 +0100 Subject: [PATCH] [#913] added groupings to hex dump --- src/core/operations/ToHexdump.mjs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/core/operations/ToHexdump.mjs b/src/core/operations/ToHexdump.mjs index ffe7131c..54cbc607 100644 --- a/src/core/operations/ToHexdump.mjs +++ b/src/core/operations/ToHexdump.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import Utils from "../Utils.mjs"; /** @@ -39,6 +40,11 @@ class ToHexdump extends Operation { "name": "Include final length", "type": "boolean", "value": false + }, + { + "name": "Output grouping", + "type": "number", + "value": 1 } ]; } @@ -52,13 +58,20 @@ class ToHexdump extends Operation { const data = new Uint8Array(input); const [length, upperCase, includeFinalLength] = args; const padding = 2; + const groupSize = args[3] || 1; + if (length%groupSize !== 0){ + throw new OperationError("The value of the width parameter must be divisible by the value of the output grouping parameter"); + } let output = ""; for (let i = 0; i < data.length; i += length) { const buff = data.slice(i, i+length); let hexa = ""; for (let j = 0; j < buff.length; j++) { - hexa += Utils.hex(buff[j], padding) + " "; + hexa += Utils.hex(buff[j], padding); + if (groupSize === 1 || (j > 0 && (j+1)%groupSize === 0)){ + hexa += " "; + } } let lineNo = Utils.hex(i, 8); @@ -69,7 +82,7 @@ class ToHexdump extends Operation { } output += lineNo + " " + - hexa.padEnd(length*(padding+1), " ") + + hexa.padEnd(length*padding+(length/groupSize), " ") + " |" + Utils.printable(Utils.byteArrayToChars(buff)).padEnd(buff.length, " ") + "|\n"; if (includeFinalLength && i+buff.length === data.length) {