// Copyright 2016 Tom Thorogood. All rights reserved.// Use of this source code is governed by a// Modified BSD License license that can be found in// the LICENSE file.//// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
// Package hex is an efficient hexadecimal implementation for Golang.
package heximport ()varerrLength = errors.New("go-hex: odd length hex string")var (lower = []byte("0123456789abcdef")upper = []byte("0123456789ABCDEF"))// InvalidByteError values describe errors resulting from an invalid byte in a hex string.typeInvalidByteErrorbytefunc ( InvalidByteError) () string {returnfmt.Sprintf("go-hex: invalid byte: %#U", rune())}// EncodedLen returns the length of an encoding of n source bytes.func ( int) int {return * 2}// DecodedLen returns the length of a decoding of n source bytes.func ( int) int {return / 2}// 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 lowercase hexadecimal encoding.func (, []byte) int {returnRawEncode(, , lower)}// EncodeUpper 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)).// EncodeUpper implements uppercase hexadecimal encoding.func (, []byte) int {returnRawEncode(, , upper)}// EncodeToString returns the lowercase hexadecimal encoding of src.func ( []byte) string {returnRawEncodeToString(, lower)}// EncodeUpperToString returns the uppercase hexadecimal encoding of src.func ( []byte) string {returnRawEncodeToString(, upper)}// RawEncodeToString returns the hexadecimal encoding of src for a given// alphabet.func (, []byte) string { := make([]byte, EncodedLen(len()))RawEncode(, , )returnstring()}// DecodeString returns the bytes represented by the hexadecimal string s.func ( string) ([]byte, error) { := []byte() := make([]byte, DecodedLen(len()))if , := Decode(, ); != nil {returnnil, }return , nil}// MustDecodeString is like DecodeString but panics if the string cannot be// parsed. It simplifies safe initialization of global variables holding// binary data.func ( string) []byte { , := DecodeString()if != nil {panic() }return}func (, , []byte) {for , := range { [*2] = [>>4] [*2+1] = [&0x0f] }}func (, []byte) (uint64, bool) {for := 0; < len()/2; ++ { , := fromHexChar([*2])if ! {returnuint64( * 2), false } , := fromHexChar([*2+1])if ! {returnuint64(*2 + 1), false } [] = ( << 4) | }return0, true}// fromHexChar converts a hex character into its value and a success flag.func ( byte) (byte, bool) {switch {case'0' <= && <= '9':return - '0', truecase'a' <= && <= 'f':return - 'a' + 10, truecase'A' <= && <= 'F':return - 'A' + 10, true }return0, false}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)