本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.1KB

  1. package binary
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. // Write writes the binary representation of data into w, using BigEndian order
  7. // https://golang.org/pkg/encoding/binary/#Write
  8. func Write(w io.Writer, data ...interface{}) error {
  9. for _, v := range data {
  10. if err := binary.Write(w, binary.BigEndian, v); err != nil {
  11. return err
  12. }
  13. }
  14. return nil
  15. }
  16. func WriteVariableWidthInt(w io.Writer, n int64) error {
  17. buf := []byte{byte(n & 0x7f)}
  18. n >>= 7
  19. for n != 0 {
  20. n--
  21. buf = append([]byte{0x80 | (byte(n & 0x7f))}, buf...)
  22. n >>= 7
  23. }
  24. _, err := w.Write(buf)
  25. return err
  26. }
  27. // WriteUint64 writes the binary representation of a uint64 into w, in BigEndian
  28. // order
  29. func WriteUint64(w io.Writer, value uint64) error {
  30. return binary.Write(w, binary.BigEndian, value)
  31. }
  32. // WriteUint32 writes the binary representation of a uint32 into w, in BigEndian
  33. // order
  34. func WriteUint32(w io.Writer, value uint32) error {
  35. return binary.Write(w, binary.BigEndian, value)
  36. }
  37. // WriteUint16 writes the binary representation of a uint16 into w, in BigEndian
  38. // order
  39. func WriteUint16(w io.Writer, value uint16) error {
  40. return binary.Write(w, binary.BigEndian, value)
  41. }
上海开阖软件有限公司 沪ICP备12045867号-1