import SwiftUI struct HexView: View { let data: Data @State private var showAll = false private var displayData: Data { if showAll || data.count <= 1024 { return data } return data.prefix(1024) } var body: some View { VStack(alignment: .leading, spacing: 0) { // Header Text("Offset 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ASCII") .font(.system(.caption2, design: .monospaced)) .foregroundStyle(.secondary) .padding(.bottom, 4) // Hex rows let bytes = [UInt8](displayData) let rowCount = (bytes.count + 15) / 16 ForEach(0.. 1024 { Button { showAll = true } label: { Text("Show All (\(formatBytes(data.count)) total)") .font(.system(.caption, design: .monospaced)) } .padding(.top, 8) } } .textSelection(.enabled) } private func hexRow(bytes: [UInt8], row: Int) -> some View { let offset = row * 16 let end = min(offset + 16, bytes.count) let rowBytes = Array(bytes[offset.. String { var result = "" for i in 0..<16 { if i < bytes.count { result += String(format: "%02X ", bytes[i]) } else { result += " " } if i == 7 { result += " " } } return result } private func asciiString(_ bytes: [UInt8]) -> String { var result = "" for byte in bytes { if byte >= 0x20, byte <= 0x7E { result += String(UnicodeScalar(byte)) } else { result += "." } } return result } private func formatBytes(_ count: Int) -> String { if count < 1024 { return "\(count) B" } if count < 1_048_576 { return String(format: "%.1f KB", Double(count) / 1024) } return String(format: "%.1f MB", Double(count) / 1_048_576) } }