hexutil

package
v0.0.0-...-39cdb81 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2019 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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\""}
	ErrLeadingZero   = &decError{"hex number with leading zero digits"}
	ErrUint64Range   = &decError{"hex number > 64 bits"}
	ErrUintRange     = &decError{fmt.Sprintf("hex number > %d bits", uintBits)}
	ErrBig256Range   = &decError{"hex number > 256 bits"}
)

错误

Functions

func Decode

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

decode解码带0x前缀的十六进制字符串。

func DecodeBig

func DecodeBig(input string) (*big.Int, error)

decodebig将前缀为0x的十六进制字符串解码为一个数量。 不接受大于256位的数字。

func DecodeUint64

func DecodeUint64(input string) (uint64, error)

decodeuint64将前缀为0x的十六进制字符串解码为一个数量。

func Encode

func Encode(b []byte) string

将B编码为带0x前缀的十六进制字符串。

func EncodeBig

func EncodeBig(bigint *big.Int) string

encodebig将bigint编码为带0x前缀的十六进制字符串。 整数的符号被忽略。

func EncodeUint64

func EncodeUint64(i uint64) string

encodeuint64将i编码为带0x前缀的十六进制字符串。

func MustDecode

func MustDecode(input string) []byte

must decode解码带0x前缀的十六进制字符串。它会因输入无效而恐慌。

func MustDecodeBig

func MustDecodeBig(input string) *big.Int

mustdecodebig将前缀为0x的十六进制字符串解码为一个数量。 它会因输入无效而恐慌。

func MustDecodeUint64

func MustDecodeUint64(input string) uint64

mustdecodeuint64将前缀为0x的十六进制字符串解码为一个数量。 它会因输入无效而恐慌。

func UnmarshalFixedJSON

func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error

unmarshalfixedjson将输入解码为带0x前缀的字符串。输出长度 确定所需的输入长度。此函数通常用于实现 固定大小类型的unmashaljson方法。

func UnmarshalFixedText

func UnmarshalFixedText(typname string, input, out []byte) error

unmarshalfixedtext将输入解码为带0x前缀的字符串。输出长度 确定所需的输入长度。此函数通常用于实现 为固定大小类型取消标记文本方法。

Example
//此源码被清华学神尹成大魔王专业翻译分析并修改
//尹成QQ77025077
//尹成微信18510341407
//尹成所在QQ群721929980
//尹成邮箱 yinc13@mails.tsinghua.edu.cn
//尹成毕业于清华大学,微软区块链领域全球最有价值专家
//https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
//版权所有2017 Go Ethereum作者
//此文件是Go以太坊库的一部分。
//
//Go-Ethereum库是免费软件:您可以重新分发它和/或修改
//根据GNU发布的较低通用公共许可证的条款
//自由软件基金会,或者许可证的第3版,或者
//(由您选择)任何更高版本。
//
//Go以太坊图书馆的发行目的是希望它会有用,
//但没有任何保证;甚至没有
//适销性或特定用途的适用性。见
//GNU较低的通用公共许可证,了解更多详细信息。
//
//你应该收到一份GNU较低级别的公共许可证副本
//以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。

package main

import (
	"encoding/json"
	"fmt"

	"github.com/ethereum/go-ethereum/common/hexutil"
)

type MyType [5]byte

func (v *MyType) UnmarshalText(input []byte) error {
	return hexutil.UnmarshalFixedText("MyType", input, v[:])
}

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)
	// 输出:
	// v1错误:十六进制字符串的长度为2,MyType需要10
	// V2错误:<nIL>
	// 版本2:0x0101010101
}
Output:

func UnmarshalFixedUnprefixedText

func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error

unmarshalfixedUnprefixedText将输入解码为带可选0x前缀的字符串。这个 输出长度决定所需的输入长度。此函数通常用于 为固定大小类型实现UnmarshalText方法。

Types

type Big

type Big big.Int

大封送/取消封送作为带有0x前缀的JSON字符串。 零值封送为“0x0”。

此时不支持负整数。试图封送他们将 返回一个错误。大于256位的值将被取消标记拒绝,但将 已正确封送。

func (Big) MarshalText

func (b Big) MarshalText() ([]byte, error)

MarshalText实现Encoding.TextMarshaler

func (*Big) String

func (b *Big) String() string

字符串返回b的十六进制编码。

func (*Big) ToInt

func (b *Big) ToInt() *big.Int

ToInt将b转换为big.int。

func (*Big) UnmarshalJSON

func (b *Big) UnmarshalJSON(input []byte) error

unmashaljson实现json.unmasheler。

func (*Big) UnmarshalText

func (b *Big) UnmarshalText(input []byte) error

UnmarshalText实现编码。textUnmarshaller

type Bytes

type Bytes []byte

字节封送/取消封送为带0x前缀的JSON字符串。 空切片封送为“0x”。

func (Bytes) MarshalText

func (b Bytes) MarshalText() ([]byte, error)

MarshalText实现Encoding.TextMarshaler

func (Bytes) String

func (b Bytes) String() string

字符串返回b的十六进制编码。

func (*Bytes) UnmarshalJSON

func (b *Bytes) UnmarshalJSON(input []byte) error

unmashaljson实现json.unmasheler。

func (*Bytes) UnmarshalText

func (b *Bytes) UnmarshalText(input []byte) error

UnmarshalText实现encoding.textUnmarshaller。

type Uint

type Uint uint

uint将封送/取消封送为前缀为0x的JSON字符串。 零值封送为“0x0”。

func (Uint) MarshalText

func (b Uint) MarshalText() ([]byte, error)

MarshalText实现Encoding.TextMarshaler。

func (Uint) String

func (b Uint) String() string

字符串返回b的十六进制编码。

func (*Uint) UnmarshalJSON

func (b *Uint) UnmarshalJSON(input []byte) error

unmashaljson实现json.unmasheler。

func (*Uint) UnmarshalText

func (b *Uint) UnmarshalText(input []byte) error

UnmarshalText实现encoding.textUnmarshaller。

type Uint64

type Uint64 uint64

uint64以带有0x前缀的JSON字符串封送/取消封送。 零值封送为“0x0”。

func (Uint64) MarshalText

func (b Uint64) MarshalText() ([]byte, error)

MarshalText实现Encoding.TextMarshaler。

func (Uint64) String

func (b Uint64) String() string

字符串返回b的十六进制编码。

func (*Uint64) UnmarshalJSON

func (b *Uint64) UnmarshalJSON(input []byte) error

unmashaljson实现json.unmasheler。

func (*Uint64) UnmarshalText

func (b *Uint64) UnmarshalText(input []byte) error

UnmarshalText实现编码。textUnmarshaller

Jump to

Keyboard shortcuts

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