hex

Imports

Imports #

"errors"
"fmt"
"io"
"slices"
"strings"

Constants & Variables

ErrLength var #

ErrLength reports an attempt to decode an odd-length input using [Decode] or [DecodeString]. The stream-based Decoder returns [io.ErrUnexpectedEOF] instead of ErrLength.

var ErrLength = *ast.CallExpr

bufferSize const #

bufferSize is the number of hexadecimal characters to buffer in encoder and decoder.

const bufferSize = 1024

hextable const #

const hextable = "0123456789abcdef"

reverseHexTable const #

const reverseHexTable = *ast.BinaryExpr

Type Aliases

InvalidByteError type #

InvalidByteError values describe errors resulting from an invalid byte in a hex string.

type InvalidByteError byte

Structs

decoder struct #

type decoder struct {
r io.Reader
err error
in []byte
arr [bufferSize]byte
}

dumper struct #

type dumper struct {
w io.Writer
rightChars [18]byte
buf [14]byte
used int
n uint
closed bool
}

encoder struct #

type encoder struct {
w io.Writer
err error
out [bufferSize]byte
}

Functions

AppendDecode function #

AppendDecode appends the hexadecimally decoded src to dst and returns the extended buffer. If the input is malformed, it returns the partially decoded src and an error.

func AppendDecode(dst []byte, src []byte) ([]byte, error)

AppendEncode function #

AppendEncode appends the hexadecimally encoded src to dst and returns the extended buffer.

func AppendEncode(dst []byte, src []byte) []byte

Close method #

func (h *dumper) Close() (err error)

Decode function #

Decode decodes src into [DecodedLen](len(src)) bytes, returning the actual number of bytes written to dst. Decode expects that src contains only hexadecimal characters and that src has even length. If the input is malformed, Decode returns the number of bytes decoded before the error.

func Decode(dst []byte, src []byte) (int, error)

DecodeString function #

DecodeString returns the bytes represented by the hexadecimal string s. DecodeString expects that src contains only hexadecimal characters and that src has even length. If the input is malformed, DecodeString returns the bytes decoded before the error.

func DecodeString(s string) ([]byte, error)

DecodedLen function #

DecodedLen returns the length of a decoding of x source bytes. Specifically, it returns x / 2.

func DecodedLen(x int) int

Dump function #

Dump returns a string that contains a hex dump of the given data. The format of the hex dump matches the output of `hexdump -C` on the command line.

func Dump(data []byte) string

Dumper function #

Dumper returns a [io.WriteCloser] that writes a hex dump of all written data to w. The format of the dump matches the output of `hexdump -C` on the command line.

func Dumper(w io.Writer) io.WriteCloser

Encode function #

Encode encodes src into [EncodedLen](len(src)) bytes of dst. As a convenience, it returns the number of bytes written to dst, but this value is always [EncodedLen](len(src)). Encode implements hexadecimal encoding.

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

EncodeToString function #

EncodeToString returns the hexadecimal encoding of src.

func EncodeToString(src []byte) string

EncodedLen function #

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

func EncodedLen(n int) int

Error method #

func (e InvalidByteError) Error() string

NewDecoder function #

NewDecoder returns an [io.Reader] that decodes hexadecimal characters from r. NewDecoder expects that r contain only an even number of hexadecimal characters.

func NewDecoder(r io.Reader) io.Reader

NewEncoder function #

NewEncoder returns an [io.Writer] that writes lowercase hexadecimal characters to w.

func NewEncoder(w io.Writer) io.Writer

Read method #

func (d *decoder) Read(p []byte) (n int, err error)

Write method #

func (e *encoder) Write(p []byte) (n int, err error)

Write method #

func (h *dumper) Write(data []byte) (n int, err error)

toChar function #

func toChar(b byte) byte

Generated with Arrow