hex

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hex provides the common hex manipulation functionality.

Index

Examples

Constants

View Source
const Prefix = "0x"

Variables

View Source
var ErrInvalidHexString = errors.New("invalid hex string")

Functions

func DecodeHexToBig

func DecodeHexToBig(hex string) (*big.Int, error)

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

func DecodeHexToUint(hex string) (uint64, error)

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

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

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

func FromBigToHex(num *big.Int) string

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

func FromHexString(input string) ([]byte, error)

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

func FromUintToHex(num uint64) string

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

func ToHexString(input []byte) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL