numbers.go 864 B

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