bytes.go 811 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package serial
  2. import (
  3. "encoding/hex"
  4. "strings"
  5. )
  6. func ByteToHexString(value byte) string {
  7. return hex.EncodeToString([]byte{value})
  8. }
  9. func BytesToUint16(value []byte) uint16 {
  10. return uint16(value[0])<<8 | uint16(value[1])
  11. }
  12. func BytesToUint32(value []byte) uint32 {
  13. return uint32(value[0])<<24 |
  14. uint32(value[1])<<16 |
  15. uint32(value[2])<<8 |
  16. uint32(value[3])
  17. }
  18. func BytesToInt64(value []byte) int64 {
  19. return int64(value[0])<<56 |
  20. int64(value[1])<<48 |
  21. int64(value[2])<<40 |
  22. int64(value[3])<<32 |
  23. int64(value[4])<<24 |
  24. int64(value[5])<<16 |
  25. int64(value[6])<<8 |
  26. int64(value[7])
  27. }
  28. func BytesToHexString(value []byte) string {
  29. strs := make([]string, len(value))
  30. for i, b := range value {
  31. strs[i] = hex.EncodeToString([]byte{b})
  32. }
  33. return "[" + strings.Join(strs, ",") + "]"
  34. }