byte_base10.go 393 B

1234567891011121314151617181920212223242526272829
  1. package protocol
  2. import (
  3. "errors"
  4. )
  5. var errBase10 = errors.New("failed to convert to Base10")
  6. func ByteToBase10(b []byte) (n uint64, err error) {
  7. base := uint64(10)
  8. n = 0
  9. for i := 0; i < len(b); i++ {
  10. var v byte
  11. d := b[i]
  12. switch {
  13. case '0' <= d && d <= '9':
  14. v = d - '0'
  15. default:
  16. n = 0
  17. err = errBase10
  18. return
  19. }
  20. n *= base
  21. n += uint64(v)
  22. }
  23. return n, err
  24. }