IsHex and StripHex

This commit is contained in:
rigel rozanski 2017-06-05 15:50:11 -04:00
parent a24a0ff003
commit 295f6c2cc6
1 changed files with 20 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package common
import (
"encoding/hex"
"fmt"
"strings"
)
@ -22,3 +23,22 @@ func LeftPadString(s string, totalLength int) string {
}
return s
}
// Returns true for non-empty hex-string prefixed with "0x"
func IsHex(s string) bool {
if len(s) > 2 && s[:2] == "0x" {
_, err := hex.DecodeString(s[2:])
if err != nil {
return false
}
return true
}
return false
}
func StripHex(s string) string {
if IsHex(s) {
return s[2:]
}
return s
}