interactive icon indicating copy to clipboard operation
interactive copied to clipboard

Add Binary Data Formatter with Hexadecimal View

Open Thorium opened this issue 2 months ago • 1 comments

Fixes #3489

Summary

This PR adds a proper default formatter for binary data (byte[] and ReadOnlyMemory) that displays data in a hexadecimal dump format with address offsets, hex values, and ASCII representation.

Motivation

Currently, byte arrays are displayed as simple lists of numbers, which makes it difficult to:

Quickly understand binary data structure

  • Identify patterns in hex values
  • See which bytes represent printable ASCII characters
  • Debug binary protocols or file formats

Output format:

00000000  48 65 6C 6C 6F 20 57 6F  72 6C 64 21 00 FF AB CD  |Hello World!....|
00000010  01 02 03 04                                       |....|

Features

  • Address offsets: 8 hex digits (e.g., 00000000, 00000010)
  • Hex values: 16 bytes per line, grouped in 8-byte chunks for readability
  • ASCII representation: Printable characters (32-126) shown, others as .
  • Proper spacing: Extra space between byte groups, aligned ASCII column
  • HTML support: Wraps output in <pre class="dni-binary"> tag
  • Unit-tests added

Design Decisions

  • 16 bytes per line: Industry standard for hex dumps (matches xxd, hex editors)
  • 8-byte grouping: Helps identify 64-bit boundaries and improves readability
  • Uppercase hex: Conventional and easier to distinguish from letters
  • Null/empty handling: Null arrays show , empty arrays show nothing
  • Non-breaking: Uses existing formatter infrastructure, no breaking changes

Thorium avatar Oct 30 '25 14:10 Thorium

The CI unit-tests Formatting_api_is_not_changed fails because there is a new class "in the public api": BinaryFormatter. Is this acceptable change, and if it is, then what should be done to get the test fixed?

Thorium avatar Oct 30 '25 17:10 Thorium