package hexdump

import "git.sr.ht/~shulhan/pakakeh.go/lib/hexdump"

Package hexdump implements reading and writing bytes from and into hexadecimal number. It support parsing output from hexdump(1) tool.

Index

Examples

Functions

func Parse

func Parse(in []byte, networkByteOrder bool) (out []byte, err error)

Parse parse the default output of hexdump(1) utility from parameter in back into stream of byte.

An example of default output of hexdump is

0000000 7865 5f70 6964 2f72 0000 0000 0000 0000
0000010 0000 0000 0000 0000 0000 0000 0000 0000
*
0000060 0000 0000 3030 3030 3537 0035 3030 3130

The first column is the address and the rest of the column is the data.

Each data column is 16-bit words in little-endian order, so in the above example, the first byte would be 65, second byte is 78 and so on. If parameter networkByteOrder (big-endian) is true, the first byte would be 78, second by is 65, and so on.

The asterisk "*" means that the values from address 0000020 to 0000050 is equal to the previous line, 0000010.

Example

Code:play 

package main

import (
	"fmt"
	"log"

	libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
	"git.sr.ht/~shulhan/pakakeh.go/lib/hexdump"
)

func main() {
	var (
		in = []byte(`0000000 6548 6c6c 2c6f 7720 726f 646c 0021`)

		out []byte
		err error
	)

	out, err = hexdump.Parse(in, false)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf(`%s`, libbytes.TrimNull(out))

}

Output:

Hello, world!

func PrettyPrint

func PrettyPrint(w io.Writer, title string, data []byte)

PrettyPrint write each byte in slice data as hexadecimal, ASCII character, and integer with 8 columns width.

Example

Code:play 

package main

import (
	"bytes"
	"fmt"

	"git.sr.ht/~shulhan/pakakeh.go/lib/hexdump"
)

func main() {
	var (
		data = []byte{1, 2, 3, 'H', 'e', 'l', 'l', 'o', 254, 255}
		bb   bytes.Buffer
	)

	hexdump.PrettyPrint(&bb, `PrettyPrint`, data)
	fmt.Println(bb.String())
}

Output:

PrettyPrint
          |  0  1  2  3  4  5  6  7 | 01234567 |   0   1   2   3   4   5   6   7 |
          |  8  9  A  B  C  D  E  F | 89ABCDEF |   8   9   A   B   C   D   E   F |
0x00000000| 01 02 03 48 65 6c 6c 6f | ...Hello |   1   2   3  72 101 108 108 111 |0
0x00000008| fe ff                   | ..       | 254 255                         |8

func Print

func Print(title string, data []byte, col int)

Print print each byte in slice as hexadecimal value into N column length.

Example

Code:play 

package main

import (
	"git.sr.ht/~shulhan/pakakeh.go/lib/hexdump"
)

func main() {
	title := `Print`
	data := []byte(`Hello, world !`)
	hexdump.Print(title, data, 5)

}

Output:

Print
   0 - 48 65 6C 6C 6F || H e l l o
   5 - 2C 20 77 6F 72 || , . w o r
  10 - 6C 64 20 21    || l d . !

Source Files

hexdump.go

Version
v0.60.0 (latest)
Published
Feb 1, 2025
Platform
linux/amd64
Imports
4 packages
Last checked
11 minutes ago

Tools for package owners.