Documentation
¶
Overview ¶
Package hex provides the common hex manipulation functionality.
Index ¶
- Constants
- Variables
- func DecodeHexToBig(hex string) (*big.Int, error)
- func DecodeHexToUint(hex string) (uint64, error)
- func DecodeString(str string) ([]byte, error)
- func FromBigToHex(num *big.Int) string
- func FromHexString(input string) ([]byte, error)
- func FromUintToHex(num uint64) string
- func ToHexString(input []byte) string
Examples ¶
Constants ¶
const Prefix = "0x"
Variables ¶
var ErrInvalidHexString = errors.New("invalid hex string")
Functions ¶
func DecodeHexToBig ¶
DecodeHexToBig decodes the given hex big number to *big.Int. Returns nil if hex is "0x"
Example ¶
var (
input = big.NewInt(100)
encoded = FromBigToHex(input)
)
decoded, err := DecodeHexToBig(encoded)
if err != nil {
panic(err)
}
fmt.Println(decoded.String())
Output: 100
func DecodeHexToUint ¶
DecodeHexToUint decodes the given hex number to a unit
Example ¶
var (
input = uint64(100)
encoded = FromUintToHex(input)
)
decoded, err := DecodeHexToUint(encoded)
if err != nil {
panic(err)
}
fmt.Println(decoded)
Output: 100
func DecodeString ¶
DecodeString is a wrapper for the standard hex.Decode string to avoid complicating imports
Example ¶
var (
input = []byte("random data")
encoded = ToHexString(input)
)
// DecodeString expects the input without the 0x prefix
decoded, err := DecodeString(
strings.TrimPrefix(encoded, "0x"),
)
if err != nil {
panic(err)
}
fmt.Println(string(decoded))
Output: random data
func FromBigToHex ¶
FromBigToHex returns the hexadecimal representation of the input big.Int (with the 0x prefix)
Example ¶
var ( input = big.NewInt(100) result = FromBigToHex(input) ) fmt.Println(result)
Output: 0x64
func FromHexString ¶
FromHexString converts an input string (with or without 0x) into a byte representation
Example ¶
var (
input = []byte("random data")
encoded = ToHexString(input)
)
result, err := FromHexString(encoded)
if err != nil {
panic(err)
}
fmt.Println(string(result))
Output: random data
func FromUintToHex ¶
FromUintToHex returns the hexadecimal representation of the input uint (with the 0x prefix)
Example ¶
var ( input = uint64(100) result = FromUintToHex(input) ) fmt.Println(result)
Output: 0x64
func ToHexString ¶
ToHexString converts the input data to a hex string representation with the 0x prefix
Example ¶
var (
input = []byte("random data")
result = ToHexString(input)
)
fmt.Println(result)
Output: 0x72616e646f6d2064617461
Types ¶
This section is empty.