numbers.go 1.2 KB

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