bytes.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package serial
  2. type Bytes interface {
  3. Bytes() []byte
  4. }
  5. type BytesLiteral []byte
  6. func (this BytesLiteral) Value() []byte {
  7. return []byte(this)
  8. }
  9. func (this BytesLiteral) Uint8Value() uint8 {
  10. return this.Value()[0]
  11. }
  12. func (this BytesLiteral) Uint16() Uint16Literal {
  13. return Uint16Literal(this.Uint16Value())
  14. }
  15. func (this BytesLiteral) Uint16Value() uint16 {
  16. value := this.Value()
  17. return uint16(value[0])<<8 + uint16(value[1])
  18. }
  19. func (this BytesLiteral) IntValue() int {
  20. value := this.Value()
  21. return int(value[0])<<24 + int(value[1])<<16 + int(value[2])<<8 + int(value[3])
  22. }
  23. func (this BytesLiteral) Uint32Value() uint32 {
  24. value := this.Value()
  25. return uint32(value[0])<<24 +
  26. uint32(value[1])<<16 +
  27. uint32(value[2])<<8 +
  28. uint32(value[3])
  29. }
  30. func (this BytesLiteral) Int64Value() int64 {
  31. value := this.Value()
  32. return int64(value[0])<<56 +
  33. int64(value[1])<<48 +
  34. int64(value[2])<<40 +
  35. int64(value[3])<<32 +
  36. int64(value[4])<<24 +
  37. int64(value[5])<<16 +
  38. int64(value[6])<<8 +
  39. int64(value[7])
  40. }
  41. // String returns a string presentation of this ByteLiteral
  42. func (this BytesLiteral) String() string {
  43. return string(this.Value())
  44. }
  45. // All returns true if all bytes in the ByteLiteral are the same as given value.
  46. func (this BytesLiteral) All(v byte) bool {
  47. for _, b := range this {
  48. if b != v {
  49. return false
  50. }
  51. }
  52. return true
  53. }