Documentation
¶
Overview ¶
Display binary contents in hexadecimal, decimal, octal, or ascii.
Index ¶
- Constants
- Variables
- func AsDeref[T, S any](p *S, x ...Option) (err error)
- func Bytes(b []byte, x ...Option) (err error)
- func Deref[T any](p *T, x ...Option) (err error)
- func Seq[T any](i iter.Seq[T], x ...Option) (err error)
- func Slices[T any](s []T, x ...Option) error
- func Stream(r io.Reader, x ...Option) (err error)
- func String(s string, x ...Option) error
- func Value[T any](v T, x ...Option) (err error)
- type ColorMode
- type ColorTheme
- type DisplayStyle
- type Dumper
- type Formatter
- type Option
- func ByteOrder(b binary.ByteOrder) Option
- func Color(c ColorMode) Option
- func Length(n int64) Option
- func LineWidth(n int) Option
- func Output(w io.Writer) Option
- func Range(start, end int64) Option
- func Skip(n int64) Option
- func Start(off int64) Option
- func Style(s DisplayStyle) Option
- func Theme(t *ColorTheme) Option
Examples ¶
Constants ¶
const DefaultLineWidth = 16
Variables ¶
var ( Stdout = Output(os.Stdout) // Output to stdout. Stderr = Output(os.Stderr) // Output to stderr. AutoColor = Color(ColorAuto) // Auto color mode. AlwaysColor = Color(ColorAlways) // Always color mode. NeverColor = Color(ColorNever) // Never color mode. Canonical = Style(StyleCanonical) // Canonical hex+ASCII display. OneByteChar = Style(StyleOneByteChar) // One-byte character display. OneByteHex = Style(StyleOneByteHex) // One-byte hex display. OneByteOctal = Style(StyleOneByteOctal) // One-byte octal display. TwoBytesDec = Style(StyleTwoBytesDec) // Two-byte decimal display. TwoBytesHex = Style(StyleTwoBytesHex) // Two-byte hexadecimal display TwoBytesOctal = Style(StyleTwoBytesOctal) // Wwo-byte octal display LittleEndian = ByteOrder(binary.LittleEndian) // Little-endian byte order. BigEndian = ByteOrder(binary.BigEndian) // Big-endian byte order. NativeEndian = ByteOrder(binary.NativeEndian) // Native-endian byte order. )
Functions ¶
func AsDeref ¶
AsDeref converts the contents of the value pointed to by 'p' into a readable ASCII table using the options provided.
Example ¶
var b [8]byte binary.BigEndian.PutUint64(b[:], 0x123456789abcdef) _ = hexdump.AsDeref[uint64](&b[0])
Output: 00000000 01 23 45 67 89 ab cd ef |.#Eg.... |
func Bytes ¶
Bytes converts a byte slice into a readable ASCII table using the provided options. It writes the output to the specified io.Writer (default is os.Stdout).
The function accepts a byte slice 'b' and a variable number of options 'x'. The options can be used to customize the behavior of the dumper.
The function returns an error if any error occurs during the dumping process.
Example ¶
_ = hexdump.Bytes(fnv.New128().Sum([]byte("Hello, World!")))
Output: 00000000 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 6c 62 27 |Hello, World!lb'| 00000010 2e 07 bb 01 42 62 b8 21 75 62 95 c5 8d |....Bb.!ub... |
func Deref ¶
Deref converts the contents of the value pointed to by 'p' into a readable ASCII table using the options provided.
Example ¶
type UDP struct { SrcPort uint16 DstPort uint16 Length uint16 Chksum uint16 } htons := func(v uint16) uint16 { var b [2]byte binary.NativeEndian.PutUint16(b[:], v) return binary.BigEndian.Uint16(b[:]) } udp := &UDP{ SrcPort: htons(0x1234), DstPort: htons(0x5678), Length: htons(0x9abc), Chksum: htons(0xdef0), } _ = hexdump.Deref(udp)
Output: 00000000 12 34 56 78 9a bc de f0 |.4Vx.... |
func Seq ¶
Seq reads value from iter.Seq and converts it as binary content into a readable ASCII table. The function writes the output to the specified io.Writer (default is os.Stdout).
The function accepts an iterator 'i' and a variable number of options 'x'. The options can be used to customize the behavior of the dumper.
The function returns an error if any error occurs during the dumping process.
func Slices ¶
Slices converts a slice of T into a readable ASCII table using the provided options.
Example ¶
_ = hexdump.Slices([]uint16{0x0123, 0x4567, 0x89AB, 0xCDEF}, hexdump.TwoBytesHex, hexdump.LittleEndian)
Output: 00000000 0123 4567 89ab cdef |#.gE.... |
func Stream ¶
Stream reads from the provided io.Reader and converts the binary content into a readable ASCII table. The function writes the output to the specified io.Writer (default is os.Stdout).
The function accepts a byte stream 'r' and a variable number of options 'x'. The options can be used to customize the behavior of the dumper.
The function returns an error if any error occurs during the dumping process.
Example ¶
b := fnv.New128().Sum([]byte("Hello, World!")) _ = hexdump.Stream(bytes.NewBuffer(b))
Output: 00000000 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 6c 62 27 |Hello, World!lb'| 00000010 2e 07 bb 01 42 62 b8 21 75 62 95 c5 8d |....Bb.!ub... |
Types ¶
type ColorMode ¶
type ColorMode int //nolint:recvcheck
func (ColorMode) MarshalText ¶
func (*ColorMode) UnmarshalText ¶
type ColorTheme ¶
type DisplayStyle ¶
type DisplayStyle int
The display style of binary content.
const ( StyleCanonical DisplayStyle = iota // Canonical hex+ASCII display. StyleOneByteChar // One-byte character display. StyleOneByteHex // One-byte hex display. StyleOneByteOctal // One-byte octal display. StyleTwoBytesDec // Two-byte decimal display. StyleTwoBytesHex // Two-byte hexadecimal display StyleTwoBytesOctal // Wwo-byte octal display )
type Dumper ¶
type Dumper struct { // The output stream, the default is [os.Stdout]. Output io.Writer // The number of bytes per line, the default is 16. LineWidth int // The color mode of the line, the default is [ColorAuto]. Color ColorMode // The color theme of the line, the default is [DefaultTheme]. Theme *ColorTheme // The display style of the line, the default is [StyleCanonical]. Style DisplayStyle // The byte order used to read the data group, the default is [binary.NativeEndian]. ByteOrder binary.ByteOrder // The start offset of the binary content. Start int64 // Skip offset bytes from the beginning of the input. Skip int64 // Interpret only length bytes of input. Length int64 // contains filtered or unexported fields }
Dumper converts the binary content into a readable ASCII table.
func (*Dumper) Write ¶
Write writes the contents of p into the buffer.
It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short.
type Formatter ¶
type Formatter struct { *bufio.Writer *ColorTheme DisplayStyle binary.ByteOrder LineWidth int }
type Option ¶
type Option func(*Dumper)
Option can be used to customize the behavior of the Dumper.
func ByteOrder ¶
The byte order used to read the data group, the default is binary.NativeEndian.
func Length ¶
Interpret only length bytes of input.
Example ¶
_ = hexdump.String("Hello, World!", hexdump.Length(5))
Output: 00000000 48 65 6c 6c 6f |Hello |
func LineWidth ¶
The number of bytes per line, the default is 16.
Example ¶
_ = hexdump.String("Hello, World!", hexdump.LineWidth(8))
Output: 00000000 48 65 6c 6c 6f 2c 20 57 |Hello, W| 00000008 6f 72 6c 64 21 |orld! |
func Output ¶
The output stream, the default is os.Stdout.
Example ¶
var b strings.Builder _ = hexdump.String("Hello, World!", hexdump.Output(&b)) fmt.Print(b.String())
Output: 00000000 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 |Hello, World! |
func Range ¶
Extract the range of input from start to end.
Example ¶
_ = hexdump.String("Hello, World!", hexdump.Range(7, 12))
Output: 00000000 57 6f 72 6c 64 | World |
func Skip ¶
Skip offset bytes from the beginning of the input.
Example ¶
_ = hexdump.String("Hello, World!", hexdump.Skip(7))
Output: 00000000 57 6f 72 6c 64 21 | World! |
Example (Length) ¶
_ = hexdump.String("Hello, World!", hexdump.Skip(7), hexdump.Length(5))
Output: 00000000 57 6f 72 6c 64 | World |
func Start ¶
The start offset of the binary content.
Example ¶
_ = hexdump.String("Hello, World!", hexdump.Start(0x1004))
Output: 00001000 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 | Hello, World| 00001010 21 |! |
func Style ¶
func Style(s DisplayStyle) Option
The display style of the line, the default is StyleCanonical.
func Theme ¶
func Theme(t *ColorTheme) Option
The color theme of the line, the default is DefaultTheme.