numbers.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Uint32Literal uint32
  38. func (this Uint32Literal) String() string {
  39. return strconv.FormatUint(uint64(this.Value()), 10)
  40. }
  41. func (this Uint32Literal) Value() uint32 {
  42. return uint32(this)
  43. }
  44. func (this Uint32Literal) Bytes() []byte {
  45. value := this.Value()
  46. return []byte{
  47. byte(value >> 24),
  48. byte(value >> 16),
  49. byte(value >> 8),
  50. byte(value),
  51. }
  52. }
  53. type Int64Literal int64
  54. func (this Int64Literal) String() string {
  55. return strconv.FormatInt(this.Value(), 10)
  56. }
  57. func (this Int64Literal) Value() int64 {
  58. return int64(this)
  59. }
  60. func (this Int64Literal) Bytes() []byte {
  61. value := this.Value()
  62. return []byte{
  63. byte(value >> 56),
  64. byte(value >> 48),
  65. byte(value >> 40),
  66. byte(value >> 32),
  67. byte(value >> 24),
  68. byte(value >> 16),
  69. byte(value >> 8),
  70. byte(value),
  71. }
  72. }