38 lines
897 B
Go
38 lines
897 B
Go
|
package types
|
||
|
|
||
|
import "encoding/hex"
|
||
|
|
||
|
// has0xPrefix validates str begins with '0x' or '0X'.
|
||
|
func has0xPrefix(str string) bool {
|
||
|
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
|
||
|
}
|
||
|
|
||
|
// isHexCharacter returns bool of c being a valid hexadecimal.
|
||
|
func isHexCharacter(c byte) bool {
|
||
|
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
|
||
|
}
|
||
|
|
||
|
// isHex validates whether each byte is valid hexadecimal string.
|
||
|
func isHex(str string) bool {
|
||
|
if len(str)%2 != 0 {
|
||
|
return false
|
||
|
}
|
||
|
for _, c := range []byte(str) {
|
||
|
if !isHexCharacter(c) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// Bytes2Hex returns the hexadecimal encoding of d.
|
||
|
func Bytes2Hex(d []byte) string {
|
||
|
return hex.EncodeToString(d)
|
||
|
}
|
||
|
|
||
|
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
|
||
|
func Hex2Bytes(str string) []byte {
|
||
|
h, _ := hex.DecodeString(str)
|
||
|
return h
|
||
|
}
|