fromBytes
Decodes a byte array to hex, number, boolean, or string
Loading...
Decodes a byte array to hex, number, boolean, or string
Decodes a byte array to hex, number, boolean, or string using a fluent API.
import "github.com/ChefBingbong/viem-go/utils/encoding"import "github.com/ChefBingbong/viem-go/utils/encoding"import "math/big"
// Convert bytes to hexhex, _ := encoding.FromBytes([]byte{72, 101, 108, 108, 111}).ToHex()// "0x48656c6c6f"
// Convert bytes to big.Intnum, _ := encoding.FromBytes([]byte{1, 164}).ToBigInt()// big.Int representing 420
// Convert bytes to int64num, _ := encoding.FromBytes([]byte{1, 164}).ToNumber()// int64(420)
// Convert bytes to uint64num, _ := encoding.FromBytes([]byte{1, 164}).ToUint()// uint64(420)
// Convert bytes to booleanb, _ := encoding.FromBytes([]byte{1}).ToBool()// true
// Convert bytes to stringstr, _ := encoding.FromBytes([]byte{72, 101, 108, 108, 111}).ToString()// "Hello"
// With size validationhex, _ := encoding.FromBytes([]byte{0x01}).WithSize(4).ToHex()// "0x00000001" (padded)string, *big.Int, int64, uint64, bool, error)The decoded value in the requested format.
[]byteThe byte array to decode.
hex, _ := encoding.FromBytes([]byte{72, 101, 108, 108, 111}).ToHex()intValidates that the bytes don't exceed the specified size.
// Validate sizehex, err := encoding.FromBytes([]byte{0x01, 0x02, 0x03}).WithSize(2).ToHex()// error: size overflowTreats the bytes as a signed integer (two's complement) for ToBigInt and ToNumber conversions.
// Convert signed bytesnum, _ := encoding.FromBytes([]byte{0xff, 0xff, 0xff, 0xff}). WithSize(4). WithSigned(). ToBigInt()// big.Int representing -1Converts bytes to a hex string:
hex, _ := encoding.FromBytes([]byte{0x01, 0x02}).ToHex()
// "0x0102"
Converts bytes to a hex string without 0x prefix:
hex, _ := encoding.FromBytes([]byte{0x01, 0x02}).ToHexUnprefixed()
// "0102"
Converts bytes to a *big.Int:
num, _ := encoding.FromBytes([]byte{1, 164}).ToBigInt()
// big.Int representing 420
Converts bytes to an int64:
num, _ := encoding.FromBytes([]byte{1, 164}).ToNumber()
// int64(420)
Converts bytes to a uint64:
num, _ := encoding.FromBytes([]byte{1, 164}).ToUint()
// uint64(420)
Converts bytes to a boolean:
b, _ := encoding.FromBytes([]byte{1}).ToBool()
// true
b, _ = encoding.FromBytes([]byte{0}).ToBool()
// false
Converts bytes to a UTF-8 string (trims trailing zeros):
str, _ := encoding.FromBytes([]byte{72, 101, 108, 108, 111}).ToString()
// "Hello"
Converts bytes to hex:
hex := encoding.BytesToHex([]byte{0x01, 0x02})
// "0x0102"
Converts bytes to *big.Int:
num := encoding.BytesToBigInt([]byte{1, 164}, false)
// big.Int representing 420
Converts bytes to int64:
num, _ := encoding.BytesToNumber([]byte{1, 164}, false)
// int64(420)
Converts bytes to uint64:
num, _ := encoding.BytesToUint([]byte{1, 164})
// uint64(420)
Converts bytes to boolean:
b, _ := encoding.BytesToBool([]byte{1})
// true
Converts bytes to string:
str := encoding.BytesToString([]byte{72, 101, 108, 108, 111})
// "Hello"