-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex.go
49 lines (41 loc) · 1.36 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
package encoding
import (
"encoding/hex"
"fmt"
"strings"
)
// EncodeHexZeroX encodes src into "0x"+hex.Encode. As a convenience, it returns the encoding type used,
// but this value is always TypeHex0XPrefix.
// EncodeHexZeroX uses hexadecimal encoding prefixed with "0x".
func EncodeHexZeroX(src []byte) (encoded string) {
out := make([]byte, len(src)*2+2)
copy(out, "0x")
hex.Encode(out[2:], src)
return string(out)
}
// DecodeHexZeroX returns the bytes represented by the hexadecimal string src.
//
// DecodeHexZeroX expects that src contains only hex characters and must contain a `0x` prefix.
// If the input is malformed, DecodeHexZeroX returns an error.
func DecodeHexZeroX(in string) ([]byte, error) {
if in == "" {
return nil, fmt.Errorf("empty hex string")
}
if !strings.HasPrefix(in, "0x") {
return nil, fmt.Errorf("missing \"0x\" prefix from hex string")
}
return hex.DecodeString(in[2:])
}
// EncodeHex returns the hexadecimal encoding of src.
func EncodeHex(src []byte) string {
return hex.EncodeToString(src)
}
// DecodeHex returns the bytes represented by the hexadecimal string s.
//
// DecodeHex expects that src contains only hexadecimal
// characters and that src has even length.
// If the input is malformed, DecodeString returns
// the bytes decoded before the error.
func DecodeHex(s string) ([]byte, error) {
return hex.DecodeString(s)
}