Documentation
¶
Overview ¶
Package revhex implements reverse hexadecimal encoding and decoding.
The reverse hex format is just normal 16-base hexadecimal encoding but using `z-k` as the alphabet instead of `0-9a-f`.
The implementations in this package are closely modeled after the standard library encoding/hex package, and have identical APIs and performance characteristics.
Example ¶
package main import ( "encoding/hex" "fmt" "github.com/mroth/revhex" ) func main() { sample := []byte{187, 132, 192, 163, 222, 186, 197, 248} fmt.Println("bytes: ", sample) fmt.Println("hex: ", hex.EncodeToString(sample)) fmt.Println("revhex: ", revhex.EncodeToString(sample)) }
Output: bytes: [187 132 192 163 222 186 197 248] hex: bb84c0a3debac5f8 revhex: oorvnzpwmlopnukr
Index ¶
- Variables
- func AppendDecode(dst, src []byte) ([]byte, error)
- func AppendEncode(dst, src []byte) []byte
- func Decode(dst, src []byte) (int, error)
- func DecodeString(s string) ([]byte, error)
- func DecodedLen(x int) int
- func Encode(dst, src []byte) int
- func EncodeToString(src []byte) string
- func EncodedLen(n int) int
- func NewDecoder(r io.Reader) io.Reader
- func NewEncoder(w io.Writer) io.Writer
- type InvalidByteError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrLength = errors.New("encoding/revhex: odd length reverse hex string")
ErrLength reports an attempt to decode an odd-length input using Decode or DecodeString. The stream-based Decoder returns io.ErrUnexpectedEOF instead of ErrLength.
Functions ¶
func AppendDecode ¶
AppendDecode appends the reverse 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 AppendEncode ¶
AppendEncode appends the reverse hexadecimally encoded src to dst and returns the extended buffer.
func Decode ¶
Decode decodes src into DecodedLen(len(src)) bytes, returning the actual number of bytes written to dst.
Decode expects that src contains only reverse hexadecimal characters and that src has even length. If the input is malformed, Decode returns the number of bytes decoded before the error.
Example ¶
package main import ( "fmt" "log" "github.com/mroth/revhex" ) func main() { src := []byte("vrtutntntkxzvstksztrtusxxy") dst := make([]byte, revhex.DecodedLen(len(src))) n, err := revhex.Decode(dst, src) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", dst[:n]) }
Output: Hello Gopher!
func DecodeString ¶
DecodeString returns the bytes represented by the reverse hexadecimal string s.
DecodeString expects that src contains only reverse hexadecimal characters and that src has even length. If the input is malformed, DecodeString returns the bytes decoded before the error.
Example ¶
package main import ( "fmt" "log" "github.com/mroth/revhex" ) func main() { const s = "vrtutntntkxzvstksztrtusxxy" decoded, err := revhex.DecodeString(s) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", decoded) }
Output: Hello Gopher!
func DecodedLen ¶
DecodedLen returns the length of a decoding of x source bytes. Specifically, it returns x / 2.
func Encode ¶
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 reverse hexadecimal encoding.
Example ¶
package main import ( "fmt" "github.com/mroth/revhex" ) func main() { src := []byte("Hello Gopher!") dst := make([]byte, revhex.EncodedLen(len(src))) revhex.Encode(dst, src) fmt.Printf("%s\n", dst) }
Output: vrtutntntkxzvstksztrtusxxy
func EncodeToString ¶
EncodeToString returns the reverse hexadecimal encoding of src.
Example ¶
package main import ( "fmt" "github.com/mroth/revhex" ) func main() { src := []byte("Hello Gopher!") encodedStr := revhex.EncodeToString(src) fmt.Printf("%s\n", encodedStr) }
Output: vrtutntntkxzvstksztrtusxxy
func EncodedLen ¶
EncodedLen returns the length of an encoding of n source bytes. Specifically, it returns n * 2.
func NewDecoder ¶
NewDecoder returns an io.Reader that decodes reverse hexadecimal characters from r. NewDecoder expects that r contain only an even number of reverse hexadecimal characters.
Types ¶
type InvalidByteError ¶
type InvalidByteError byte
InvalidByteError values describe errors resulting from an invalid byte in a reverse hex string.
func (InvalidByteError) Error ¶
func (e InvalidByteError) Error() string