numbers.go 952 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package serial
  2. import (
  3. "strconv"
  4. )
  5. func Uint16ToBytes(value uint16, b []byte) []byte {
  6. return append(b, byte(value>>8), byte(value))
  7. }
  8. func Uint16ToString(value uint16) string {
  9. return strconv.Itoa(int(value))
  10. }
  11. func Uint32ToBytes(value uint32, b []byte) []byte {
  12. return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
  13. }
  14. func Uint32ToString(value uint32) string {
  15. return strconv.FormatUint(uint64(value), 10)
  16. }
  17. func IntToBytes(value int, b []byte) []byte {
  18. return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
  19. }
  20. func IntToString(value int) string {
  21. return Int64ToString(int64(value))
  22. }
  23. func Int64ToBytes(value int64, b []byte) []byte {
  24. return append(b,
  25. byte(value>>56),
  26. byte(value>>48),
  27. byte(value>>40),
  28. byte(value>>32),
  29. byte(value>>24),
  30. byte(value>>16),
  31. byte(value>>8),
  32. byte(value))
  33. }
  34. func Int64ToString(value int64) string {
  35. return strconv.FormatInt(value, 10)
  36. }