numbers.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package serial
  2. import (
  3. "strconv"
  4. )
  5. type Uint16 interface {
  6. Value() uint16
  7. }
  8. type Uint16Literal uint16
  9. func (this Uint16Literal) String() string {
  10. return strconv.Itoa(int(this))
  11. }
  12. func (this Uint16Literal) Value() uint16 {
  13. return uint16(this)
  14. }
  15. func (this Uint16Literal) Bytes() []byte {
  16. return []byte{byte(this >> 8), byte(this)}
  17. }
  18. type Int interface {
  19. Value() int
  20. }
  21. type IntLiteral int
  22. func (this IntLiteral) String() string {
  23. return strconv.Itoa(int(this))
  24. }
  25. func (this IntLiteral) Value() int {
  26. return int(this)
  27. }
  28. func (this IntLiteral) Bytes() []byte {
  29. value := this.Value()
  30. return []byte{
  31. byte(value >> 24),
  32. byte(value >> 16),
  33. byte(value >> 8),
  34. byte(value),
  35. }
  36. }
  37. type Int64Literal int64
  38. func (this Int64Literal) String() string {
  39. return strconv.FormatInt(this.Value(), 10)
  40. }
  41. func (this Int64Literal) Value() int64 {
  42. return int64(this)
  43. }
  44. func (this Int64Literal) Bytes() []byte {
  45. value := this.Value()
  46. return []byte{
  47. byte(value >> 56),
  48. byte(value >> 48),
  49. byte(value >> 40),
  50. byte(value >> 32),
  51. byte(value >> 24),
  52. byte(value >> 16),
  53. byte(value >> 8),
  54. byte(value),
  55. }
  56. }