hexutils.go 548 B

12345678910111213141516171819202122232425262728293031
  1. package hexutils
  2. import "github.com/sirupsen/logrus"
  3. func HexStrToHexList(hexStr string) []byte {
  4. var ok bool
  5. dst := make([]byte, len(hexStr))
  6. for i, item := range []byte(hexStr) {
  7. dst[i], ok = FromHexChar(item)
  8. if !ok {
  9. logrus.Fatal("hex str 不合法:", hexStr)
  10. continue
  11. }
  12. }
  13. return dst
  14. }
  15. func FromHexChar(c byte) (byte, bool) {
  16. switch {
  17. case '0' <= c && c <= '9':
  18. return c - '0', true
  19. case 'a' <= c && c <= 'f':
  20. return c - 'a' + 10, true
  21. case 'A' <= c && c <= 'F':
  22. return c - 'A' + 10, true
  23. }
  24. return 0, false
  25. }