Problem · Array
Hex Dump Formatter
Learn this problemProblem statement
Given an array bytes of byte values and a positive row width bytesPerRow, return a formatted hexadecimal dump as an array of rows.
Each row has this exact structure:
- an eight-character lowercase hexadecimal offset for the row's first byte;
- two spaces;
bytesPerRowuppercase two-digit hexadecimal slots separated by one space, with missing slots in the final row filled by spaces;- two spaces;
- an ASCII column surrounded by
|characters.
In the ASCII column, byte values from 0x20 through 0x7E use their ASCII character. Every other byte uses .. Return an empty array when bytes is empty.
Function
formatHexDump(bytes: int[], bytesPerRow: int) → String[]Examples
Example 1
bytes = [72,101,108,108,111,0,35,255]bytesPerRow = 4return = ["00000000 48 65 6C 6C |Hell|","00000004 6F 00 23 FF |o.#.|"]The first row contains four printable bytes. In the second row, 0x00 and 0xFF become ., while 0x23 becomes #.
Example 2
bytes = [65,66,67]bytesPerRow = 4return = ["00000000 41 42 43 |ABC|"]The final hexadecimal slot is blank, but the ASCII column contains only the three present bytes.
Constraints
0 <= bytes.length <= 10^50 <= bytes[i] <= 2551 <= bytesPerRow <= 32