hex.EncodeToString, hex.DecodeString
func EncodeToString(src []byte) string
func DecodeString(s string) ([]byte, error)
func EncodedLen(n int) int
func DecodedLen(x int) intEncode bytes as lowercase hex, and decode an even-length hex string back to bytes.
| Parameter | Description |
|---|---|
src / s | EncodeToString takes the bytes to encode. DecodeString takes a string of 0-9a-fA-F digits with even length. |
Returns: EncodeToString returns a string twice as long as src. DecodeString returns the decoded bytes and a nil error when s is well formed.
- AppendEncode and AppendDecode, added in Go 1.22, append to a destination slice. They are not shown here.
package main
import (
"encoding/hex"
"fmt"
)
func main() {
fmt.Println(hex.EncodeToString([]byte{0x00, 0xff, 0x10}))
out, err := hex.DecodeString("00ff10")
fmt.Printf("%x %v\n", out, err)
}Expected output
00ff10
00ff10 <nil>Related: crypto/sha256