bytes.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package utils
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. )
  7. // IntToBytes int to []bytes littleEndian
  8. func IntToBytes(n int32) []byte {
  9. x := int32(n)
  10. bytesBuffer := bytes.NewBuffer([]byte{})
  11. _ = binary.Write(bytesBuffer, binary.LittleEndian, x)
  12. return bytesBuffer.Bytes()
  13. }
  14. // UintToBytes uint to []bytes littleEndian
  15. func UintToBytes(n uint32) []byte {
  16. x := uint32(n)
  17. bytesBuffer := bytes.NewBuffer([]byte{})
  18. _ = binary.Write(bytesBuffer, binary.LittleEndian, x)
  19. return bytesBuffer.Bytes()
  20. }
  21. // UintTobytesBigEnd uint to []bytes bigEndian
  22. func UintTobytesBigEnd(n uint32) []byte {
  23. x := uint32(n)
  24. bytesBuffer := bytes.NewBuffer([]byte{})
  25. _ = binary.Write(bytesBuffer, binary.BigEndian, x)
  26. return bytesBuffer.Bytes()
  27. }
  28. // Int32TobytesBigEnd int32 to []bytes bigEndian
  29. func Int32TobytesBigEnd(n int32) []byte {
  30. bytesBuffer := bytes.NewBuffer([]byte{})
  31. _ = binary.Write(bytesBuffer, binary.BigEndian, n)
  32. return bytesBuffer.Bytes()
  33. }
  34. // Uint64ToBytes to []bytes
  35. func Uint64ToBytes(n uint64) []byte {
  36. x := n
  37. bytesBuffer := bytes.NewBuffer([]byte{})
  38. _ = binary.Write(bytesBuffer, binary.LittleEndian, x)
  39. return bytesBuffer.Bytes()
  40. }
  41. // Uint64ToBytesBigEnd to []bytes
  42. func Uint64ToBytesBigEnd(n uint64) []byte {
  43. x := n
  44. bytesBuffer := bytes.NewBuffer([]byte{})
  45. _ = binary.Write(bytesBuffer, binary.BigEndian, x)
  46. return bytesBuffer.Bytes()
  47. }
  48. // BytesToInt 字节转换成整形
  49. func BytesToInt(b []byte) int32 {
  50. bytesBuffer := bytes.NewBuffer(b)
  51. var x int32
  52. _ = binary.Read(bytesBuffer, binary.LittleEndian, &x)
  53. return x
  54. }
  55. //BytesToUint32 to []bytes
  56. func BytesToUint32(b []byte) uint32 {
  57. bytesBuffer := bytes.NewBuffer(b)
  58. var x int32
  59. _ = binary.Read(bytesBuffer, binary.LittleEndian, &x)
  60. return uint32(x)
  61. }
  62. //BytesBigEndToUint32 to uint
  63. func BytesBigEndToUint32(b []byte) uint32 {
  64. bytesBuffer := bytes.NewBuffer(b)
  65. var x int32
  66. _ = binary.Read(bytesBuffer, binary.BigEndian, &x)
  67. return uint32(x)
  68. }
  69. //Uint16TobytesBigEnd to []bytes
  70. func Uint16TobytesBigEnd(n uint16) []byte {
  71. x := uint16(n)
  72. bytesBuffer := bytes.NewBuffer([]byte{})
  73. _ = binary.Write(bytesBuffer, binary.BigEndian, x)
  74. return bytesBuffer.Bytes()
  75. }
  76. //BytesBigEndToUint16 to uint16
  77. func BytesBigEndToUint16(b []byte) uint16 {
  78. bytesBuffer := bytes.NewBuffer(b)
  79. var x int16
  80. _ = binary.Read(bytesBuffer, binary.BigEndian, &x)
  81. return uint16(x)
  82. }
  83. //BytesBigEndToUint64 to uint64
  84. func BytesBigEndToUint64(b []byte) uint64 {
  85. bytesBuffer := bytes.NewBuffer(b)
  86. var x uint64
  87. _ = binary.Read(bytesBuffer, binary.BigEndian, &x)
  88. return x
  89. }
  90. //HTONL32 转换为网络字节序
  91. func HTONL32(v uint32) []byte {
  92. return UintTobytesBigEnd(v)
  93. }
  94. //HTONLint32 转换为网络字节序
  95. func HTONLInt32(v int32) []byte {
  96. return Int32TobytesBigEnd(v)
  97. }
  98. //HTONS 转换为网络字节序
  99. func HTONS(v uint16) []byte {
  100. return Uint16TobytesBigEnd(v)
  101. }
  102. //NTOHL32 转换为主机字节序
  103. func NTOHL32(buf []byte) uint32 {
  104. return BytesBigEndToUint32(buf)
  105. }
  106. //NTOHL64 转换为主机字节序
  107. func NTOHL64(buf []byte) uint64 {
  108. return BytesBigEndToUint64(buf)
  109. }
  110. //NTOHS 主机字节序
  111. func NTOHS(buf []byte) uint16 {
  112. return BytesBigEndToUint16(buf)
  113. }
  114. //DebugString 结构体对应的Json串
  115. func DebugString(v interface{}) string {
  116. if buf, err := json.MarshalIndent(v, " ", " "); err == nil {
  117. return string(buf)
  118. }
  119. return ""
  120. }