using System.Text; namespace GsaEditor.Helpers; /// /// Produces a classic hex + ASCII dump string from a byte array. /// public static class HexDumper { /// /// Formats a byte array as a hex dump with offset, hex values, and printable ASCII characters. /// /// The data to dump. /// Maximum number of bytes to include in the dump. /// A formatted hex dump string. public static string Dump(byte[] data, int maxBytes = 512) { var sb = new StringBuilder(); int length = Math.Min(data.Length, maxBytes); const int bytesPerLine = 16; for (int offset = 0; offset < length; offset += bytesPerLine) { // Offset column sb.Append($"{offset:X8} "); int lineLen = Math.Min(bytesPerLine, length - offset); // Hex columns for (int i = 0; i < bytesPerLine; i++) { if (i < lineLen) sb.Append($"{data[offset + i]:X2} "); else sb.Append(" "); if (i == 7) sb.Append(' '); } sb.Append(' '); // ASCII column for (int i = 0; i < lineLen; i++) { byte b = data[offset + i]; sb.Append(b >= 0x20 && b < 0x7F ? (char)b : '.'); } sb.AppendLine(); } if (data.Length > maxBytes) { sb.AppendLine($"... ({data.Length - maxBytes} more bytes)"); } return sb.ToString(); } }