numbers.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 IntToString(value int) string {
  12. return Int64ToString(int64(value))
  13. }
  14. func Int64ToString(value int64) string {
  15. return strconv.FormatInt(value, 10)
  16. }
  17. type Uint16 interface {
  18. Value() uint16
  19. }
  20. type Uint16Literal uint16
  21. func (this Uint16Literal) String() string {
  22. return strconv.Itoa(int(this))
  23. }
  24. func (this Uint16Literal) Value() uint16 {
  25. return uint16(this)
  26. }
  27. func (this Uint16Literal) Bytes() []byte {
  28. return []byte{byte(this >> 8), byte(this)}
  29. }
  30. type Int interface {
  31. Value() int
  32. }
  33. type IntLiteral int
  34. func (this IntLiteral) String() string {
  35. return strconv.Itoa(int(this))
  36. }
  37. func (this IntLiteral) Value() int {
  38. return int(this)
  39. }
  40. func (this IntLiteral) Bytes() []byte {
  41. value := this.Value()
  42. return []byte{
  43. byte(value >> 24),
  44. byte(value >> 16),
  45. byte(value >> 8),
  46. byte(value),
  47. }
  48. }
  49. type Uint32Literal uint32
  50. func (this Uint32Literal) String() string {
  51. return strconv.FormatUint(uint64(this.Value()), 10)
  52. }
  53. func (this Uint32Literal) Value() uint32 {
  54. return uint32(this)
  55. }
  56. func (this Uint32Literal) Bytes() []byte {
  57. value := this.Value()
  58. return []byte{
  59. byte(value >> 24),
  60. byte(value >> 16),
  61. byte(value >> 8),
  62. byte(value),
  63. }
  64. }
  65. type Int64Literal int64
  66. func (this Int64Literal) String() string {
  67. return strconv.FormatInt(this.Value(), 10)
  68. }
  69. func (this Int64Literal) Value() int64 {
  70. return int64(this)
  71. }
  72. func (this Int64Literal) Bytes() []byte {
  73. value := this.Value()
  74. return []byte{
  75. byte(value >> 56),
  76. byte(value >> 48),
  77. byte(value >> 40),
  78. byte(value >> 32),
  79. byte(value >> 24),
  80. byte(value >> 16),
  81. byte(value >> 8),
  82. byte(value),
  83. }
  84. }