← All Exercises

Hex Dump Formatter

basicjavascript~15min

Background

hexdump-style output is the lingua franca for debugging binary formats, protocol payloads, and file corruption. The fixed layout makes diffs between dumps meaningful.

Problem

Produce a hex dump of the input text encoded as UTF-8. Each output row covers sixteen bytes: the zero-padded 8-digit hex offset, two spaces, the hex byte pairs joined by single spaces and padded to 47 characters, two more spaces, then the ASCII gutter where bytes outside printable ASCII render as a dot. Join rows with newlines.

Input

A JSON object {"text": string}.

Output

A JSON object {"dump": string} with rows joined by \n; the last row covers the remaining bytes.

Sample tests

Sample 1

Input: {"text":"Hello"}
Expected: {"dump":"00000000  48 65 6c 6c 6f                                   Hello"}

Sample 2

Input: {"text":"ABCDEFGHIJKLMNOPQRST"}
Expected: {"dump":"00000000  41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP\n00000010  51 52 53 54                                      QRST"}

Your solution

Learning objective

Practice byte-level formatting, alignment padding, and offset arithmetic in base sixteen.