Documentation ¶
Overview ¶
Package hexutil implements hex encoding with 0x prefix. This encoding is used by the Lemochain RPC API to transport binary data in JSON payloads.
Encoding Rules ¶
All hex data must have prefix "0x".
For byte slices, the hex data must be of even length. An empty byte slice encodes as "0x".
Integers are encoded using the least amount of digits (no leading zero digits). Their encoding may be of uneven length. The number zero encodes as "0x0".
Index ¶
- Variables
- func Decode(input string) ([]byte, error)
- func Encode(b []byte) string
- func MustDecode(input string) []byte
- func MustParseUint64(s string) uint64
- func ParseUint(s string, bitSize int) (uint64, error)
- func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error
- func UnmarshalFixedText(typname string, input, out []byte, want0xPrefix bool) error
- type Big
- type Big10
- type Bytes
- type IP
- type Uint16
- type Uint32
- type Uint64
- type Uint8
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyString = &decError{"empty hex string"} ErrSyntax = &decError{"invalid hex string"} ErrMissingPrefix = &decError{"hex string without 0x prefix"} ErrOddLength = &decError{"hex string of odd length"} ErrEmptyNumber = &decError{"hex string \"0x\""} ErrRange = &decError{"number is out of range"} Err256Range = &decError{"hex number > 256 bits"} )
var (
IPT = reflect.TypeOf(IP(nil))
)
Functions ¶
func MustDecode ¶
MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
func MustParseUint64 ¶
MustParseUint64 parses s as an integer and panics if the string is invalid.
func ParseUint ¶
ParseUint64 parses s as an integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.
func UnmarshalFixedJSON ¶
UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalJSON method for fixed-size types.
func UnmarshalFixedText ¶
UnmarshalFixedText decodes the input as a string. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/LemoFoundationLtd/lemochain-core/common/hexutil" ) type MyType [5]byte func (v *MyType) UnmarshalText(input []byte) error { return hexutil.UnmarshalFixedText("MyType", input, v[:], true) } func (v MyType) String() string { return hexutil.Bytes(v[:]).String() } func main() { var v1, v2 MyType fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1)) fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2)) fmt.Println("v2:", v2) }
Output: v1 error: hex string has length 2, want 10 for MyType v2 error: <nil> v2: 0x0101010101
Types ¶
type Big ¶
Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.
func (Big) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Big) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Big) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type Big10 ¶
Big10 marshals/unmarshals as a JSON decimal string. The zero value marshals as "0".
Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.
func (Big10) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Big10) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Big10) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type Bytes ¶
type Bytes []byte
Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".
func (Bytes) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Bytes) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type IP ¶
func (*IP) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*IP) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*IP) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Uint16 ¶ added in v1.2.0
type Uint16 uint16
Uint16 marshals uint16 as decimal, and unmarshals string and number as decimal or hex.
func (Uint16) MarshalText ¶ added in v1.2.0
MarshalText implements encoding.TextMarshaler.
func (*Uint16) UnmarshalJSON ¶ added in v1.2.0
UnmarshalJSON implements json.Unmarshaler.
func (*Uint16) UnmarshalText ¶ added in v1.2.0
UnmarshalText implements encoding.TextUnmarshaler.
type Uint32 ¶
type Uint32 uint32
Uint32 marshals uint32 as decimal, and unmarshals string and number as decimal or hex.
func (Uint32) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint32) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint32) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Uint64 ¶
type Uint64 uint64
Uint64 marshals uint64 as decimal, and unmarshals string and number as decimal or hex.
func (Uint64) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint64) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Uint8 ¶ added in v1.2.0
type Uint8 uint8
Uint8 marshals uint16 as decimal, and unmarshals string and number as decimal or hex.
func (Uint8) MarshalText ¶ added in v1.2.0
MarshalText implements encoding.TextMarshaler.
func (*Uint8) UnmarshalJSON ¶ added in v1.2.0
UnmarshalJSON implements json.Unmarshaler.
func (*Uint8) UnmarshalText ¶ added in v1.2.0
UnmarshalText implements encoding.TextUnmarshaler.