This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
forked from umbracle/minimal
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hex.go
89 lines (75 loc) · 1.89 KB
/
hex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package hex
import (
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"
)
var (
ErrSyntax = &DecError{"invalid hex string"}
ErrMissingPrefix = &DecError{"hex string without 0x prefix"}
ErrEmptyNumber = &DecError{"hex string \"0x\""}
ErrLeadingZero = &DecError{"hex number with leading zero digits"}
ErrUint64Range = &DecError{"hex number > 64 bits"}
ErrBig256Range = &DecError{"hex number > 256 bits"}
)
type DecError struct{ msg string }
func (err DecError) Error() string { return err.msg }
func EncodeToHex(str []byte) string {
return "0x" + hex.EncodeToString(str)
}
func EncodeToString(str []byte) string {
return hex.EncodeToString(str)
}
func DecodeString(str string) ([]byte, error) {
return hex.DecodeString(str)
}
func MustDecodeString(str string) []byte {
buf, err := DecodeString(str)
if err != nil {
panic(fmt.Errorf("could not decode string: %v", err))
}
return buf
}
func DecodeHex(str string) ([]byte, error) {
if strings.HasPrefix(str, "0x") {
str = str[2:]
}
return hex.DecodeString(str)
}
func MustDecodeHex(str string) []byte {
buf, err := DecodeHex(str)
if err != nil {
panic(fmt.Errorf("could not decode hex: %v", err))
}
return buf
}
// EncodeUint64 encodes i as a hex string with 0x prefix.
func EncodeUint64(i uint64) string {
enc := make([]byte, 2, 10)
copy(enc, "0x")
return string(strconv.AppendUint(enc, i, 16))
}
const BadNibble = ^uint64(0)
func DecodeNibble(in byte) uint64 {
switch {
case in >= '0' && in <= '9':
return uint64(in - '0')
case in >= 'A' && in <= 'F':
return uint64(in - 'A' + 10)
case in >= 'a' && in <= 'f':
return uint64(in - 'a' + 10)
default:
return BadNibble
}
}
// EncodeBig encodes bigint as a hex string with 0x prefix.
// The sign of the integer is ignored.
func EncodeBig(bigint *big.Int) string {
nbits := bigint.BitLen()
if nbits == 0 {
return "0x0"
}
return fmt.Sprintf("%#x", bigint)
}