std / encoding/hex

encoding/hex

import "std:encoding/hex"

Provides hexadecimal encoding and decoding functions. Encode/Decode operate on caller-provided buffers (zero-alloc). EncodeToString/DecodeString allocate and return new values.

View source on Codeberg →

Length

#
func EncodedLen(n int) int

Returns the length of an encoding of n source bytes. Specifically, it returns n * 2.

#
func DecodedLen(n int) int

Returns the length of a decoding of n source bytes. Specifically, it returns n / 2.

Buffer-Based

#
func Encode(dst []byte, src []byte) int

Encodes src into dst and returns the number of bytes written. dst must be at least EncodedLen(len(src)) bytes long.

#
func Decode(dst []byte, src []byte) (int, bool)

Decodes src into dst and returns the number of bytes written and a boolean indicating success. dst must be at least DecodedLen(len(src)) bytes long.

Allocating

#
func EncodeToString(src []byte) string

Returns the hexadecimal encoding of src as a newly allocated string.

#
func DecodeString(s string) (list[byte], bool)

Returns the bytes represented by the hexadecimal string s as a newly allocated dynamic byte slice, along with a boolean indicating success.