encoding/hex

Guided tour · Encoding · pkg.go.dev →

Hex encoding: []byte ↔ ASCII hex string.

One-shot encoding

EncodeToString / DecodeString

s := hex.EncodeToString([]byte{0xDE, 0xAD, 0xBE, 0xEF})
fmt.Println(s)   // "deadbeef"

b, _ := hex.DecodeString(s)
fmt.Printf("% x\n", b)

Dump — canonical hex+ASCII layout

Same formatting as 'xxd' or 'hexdump -C'.

fmt.Print(hex.Dump([]byte("Hello, Go!")))
Output
00000000  48 65 6c 6c 6f 2c 20 47  6f 21                    |Hello, Go!|

Streaming

NewEncoder / NewDecoder

w := hex.NewEncoder(os.Stdout)
w.Write([]byte("hi"))
fmt.Println()   // "6869"