numbers.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package serial
  2. import (
  3. "strconv"
  4. "v2ray.com/core/common/buf"
  5. )
  6. func Uint16ToBytes(value uint16, b []byte) []byte {
  7. return append(b, byte(value>>8), byte(value))
  8. }
  9. func Uint16ToString(value uint16) string {
  10. return strconv.Itoa(int(value))
  11. }
  12. func WriteUint16(value uint16) buf.BytesWriter {
  13. return func(b []byte) int {
  14. b = Uint16ToBytes(value, b[:0])
  15. return 2
  16. }
  17. }
  18. func Uint32ToBytes(value uint32, b []byte) []byte {
  19. return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
  20. }
  21. func Uint32ToString(value uint32) string {
  22. return strconv.FormatUint(uint64(value), 10)
  23. }
  24. func WriteUint32(value uint32) buf.BytesWriter {
  25. return func(b []byte) int {
  26. b = Uint32ToBytes(value, b[:0])
  27. return 4
  28. }
  29. }
  30. func IntToBytes(value int, b []byte) []byte {
  31. return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
  32. }
  33. func IntToString(value int) string {
  34. return Int64ToString(int64(value))
  35. }
  36. func Int64ToBytes(value int64, b []byte) []byte {
  37. return append(b,
  38. byte(value>>56),
  39. byte(value>>48),
  40. byte(value>>40),
  41. byte(value>>32),
  42. byte(value>>24),
  43. byte(value>>16),
  44. byte(value>>8),
  45. byte(value))
  46. }
  47. func Int64ToString(value int64) string {
  48. return strconv.FormatInt(value, 10)
  49. }