// 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 hex import ( ) var errLength = 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. type InvalidByteError byte func ( InvalidByteError) () string { return fmt.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 { return RawEncode(, , 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 { return RawEncode(, , upper) } // EncodeToString returns the lowercase hexadecimal encoding of src. func ( []byte) string { return RawEncodeToString(, lower) } // EncodeUpperToString returns the uppercase hexadecimal encoding of src. func ( []byte) string { return RawEncodeToString(, upper) } // RawEncodeToString returns the hexadecimal encoding of src for a given // alphabet. func (, []byte) string { := make([]byte, EncodedLen(len())) RawEncode(, , ) return string() } // DecodeString returns the bytes represented by the hexadecimal string s. func ( string) ([]byte, error) { := []byte() := make([]byte, DecodedLen(len())) if , := Decode(, ); != nil { return nil, } 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 ! { return uint64( * 2), false } , := fromHexChar([*2+1]) if ! { return uint64(*2 + 1), false } [] = ( << 4) | } return 0, true } // fromHexChar converts a hex character into its value and a success flag. func ( byte) (byte, bool) { switch { case '0' <= && <= '9': return - '0', true case 'a' <= && <= 'f': return - 'a' + 10, true case 'A' <= && <= 'F': return - 'A' + 10, true } return 0, false }