numbers.go 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package serial
  2. import "strconv"
  3. // Uint16ToBytes serializes a uint16 into bytes in big endian order.
  4. func Uint16ToBytes(value uint16, b []byte) []byte {
  5. return append(b, byte(value>>8), byte(value))
  6. }
  7. func Uint16ToString(value uint16) string {
  8. return strconv.Itoa(int(value))
  9. }
  10. func Uint32ToBytes(value uint32, b []byte) []byte {
  11. return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
  12. }
  13. func Uint32ToString(value uint32) string {
  14. return strconv.FormatUint(uint64(value), 10)
  15. }
  16. func WriteUint32(value uint32) func([]byte) (int, error) {
  17. return func(b []byte) (int, error) {
  18. Uint32ToBytes(value, b[:0])
  19. return 4, nil
  20. }
  21. }
  22. func Int64ToBytes(value int64, b []byte) []byte {
  23. return append(b,
  24. byte(value>>56),
  25. byte(value>>48),
  26. byte(value>>40),
  27. byte(value>>32),
  28. byte(value>>24),
  29. byte(value>>16),
  30. byte(value>>8),
  31. byte(value))
  32. }
  33. func Int64ToString(value int64) string {
  34. return strconv.FormatInt(value, 10)
  35. }