12345678910111213141516171819202122232425262728293031 |
- package hexutils
- import "github.com/sirupsen/logrus"
- func HexStrToHexList(hexStr string) []byte {
- var ok bool
- dst := make([]byte, len(hexStr))
- for i, item := range []byte(hexStr) {
- dst[i], ok = FromHexChar(item)
- if !ok {
- logrus.Fatal("hex str 不合法:", hexStr)
- continue
- }
- }
- return dst
- }
- func FromHexChar(c byte) (byte, bool) {
- switch {
- case '0' <= c && c <= '9':
- return c - '0', true
- case 'a' <= c && c <= 'f':
- return c - 'a' + 10, true
- case 'A' <= c && c <= 'F':
- return c - 'A' + 10, true
- }
- return 0, false
- }
|