gbinary_be.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gbinary
  7. import (
  8. "bytes"
  9. "encoding/binary"
  10. "fmt"
  11. "math"
  12. )
  13. // BeEncode encodes one or multiple <values> into bytes using BigEndian.
  14. // It uses type asserting checking the type of each value of <values> and internally
  15. // calls corresponding converting function do the bytes converting.
  16. //
  17. // It supports common variable type asserting, and finally it uses fmt.Sprintf converting
  18. // value to string and then to bytes.
  19. func BeEncode(values ...interface{}) []byte {
  20. buf := new(bytes.Buffer)
  21. for i := 0; i < len(values); i++ {
  22. if values[i] == nil {
  23. return buf.Bytes()
  24. }
  25. switch value := values[i].(type) {
  26. case int:
  27. buf.Write(BeEncodeInt(value))
  28. case int8:
  29. buf.Write(BeEncodeInt8(value))
  30. case int16:
  31. buf.Write(BeEncodeInt16(value))
  32. case int32:
  33. buf.Write(BeEncodeInt32(value))
  34. case int64:
  35. buf.Write(BeEncodeInt64(value))
  36. case uint:
  37. buf.Write(BeEncodeUint(value))
  38. case uint8:
  39. buf.Write(BeEncodeUint8(value))
  40. case uint16:
  41. buf.Write(BeEncodeUint16(value))
  42. case uint32:
  43. buf.Write(BeEncodeUint32(value))
  44. case uint64:
  45. buf.Write(BeEncodeUint64(value))
  46. case bool:
  47. buf.Write(BeEncodeBool(value))
  48. case string:
  49. buf.Write(BeEncodeString(value))
  50. case []byte:
  51. buf.Write(value)
  52. case float32:
  53. buf.Write(BeEncodeFloat32(value))
  54. case float64:
  55. buf.Write(BeEncodeFloat64(value))
  56. default:
  57. if err := binary.Write(buf, binary.BigEndian, value); err != nil {
  58. buf.Write(BeEncodeString(fmt.Sprintf("%v", value)))
  59. }
  60. }
  61. }
  62. return buf.Bytes()
  63. }
  64. // 将变量转换为二进制[]byte,并指定固定的[]byte长度返回,长度单位为字节(byte);
  65. // 如果转换的二进制长度超过指定长度,那么进行截断处理
  66. func BeEncodeByLength(length int, values ...interface{}) []byte {
  67. b := BeEncode(values...)
  68. if len(b) < length {
  69. b = append(b, make([]byte, length-len(b))...)
  70. } else if len(b) > length {
  71. b = b[0:length]
  72. }
  73. return b
  74. }
  75. // 整形二进制解包,注意第二个及其后参数为字长确定的整形变量的指针地址,以便确定解析的[]byte长度,
  76. // 例如:int8/16/32/64、uint8/16/32/64、float32/64等等
  77. func BeDecode(b []byte, values ...interface{}) error {
  78. buf := bytes.NewBuffer(b)
  79. for i := 0; i < len(values); i++ {
  80. err := binary.Read(buf, binary.BigEndian, values[i])
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. return nil
  86. }
  87. func BeEncodeString(s string) []byte {
  88. return []byte(s)
  89. }
  90. func BeDecodeToString(b []byte) string {
  91. return string(b)
  92. }
  93. func BeEncodeBool(b bool) []byte {
  94. if b == true {
  95. return []byte{1}
  96. } else {
  97. return []byte{0}
  98. }
  99. }
  100. // 自动识别int类型长度,转换为[]byte
  101. func BeEncodeInt(i int) []byte {
  102. if i <= math.MaxInt8 {
  103. return BeEncodeInt8(int8(i))
  104. } else if i <= math.MaxInt16 {
  105. return BeEncodeInt16(int16(i))
  106. } else if i <= math.MaxInt32 {
  107. return BeEncodeInt32(int32(i))
  108. } else {
  109. return BeEncodeInt64(int64(i))
  110. }
  111. }
  112. // 自动识别uint类型长度,转换为[]byte
  113. func BeEncodeUint(i uint) []byte {
  114. if i <= math.MaxUint8 {
  115. return BeEncodeUint8(uint8(i))
  116. } else if i <= math.MaxUint16 {
  117. return BeEncodeUint16(uint16(i))
  118. } else if i <= math.MaxUint32 {
  119. return BeEncodeUint32(uint32(i))
  120. } else {
  121. return BeEncodeUint64(uint64(i))
  122. }
  123. }
  124. func BeEncodeInt8(i int8) []byte {
  125. return []byte{byte(i)}
  126. }
  127. func BeEncodeUint8(i uint8) []byte {
  128. return []byte{i}
  129. }
  130. func BeEncodeInt16(i int16) []byte {
  131. b := make([]byte, 2)
  132. binary.BigEndian.PutUint16(b, uint16(i))
  133. return b
  134. }
  135. func BeEncodeUint16(i uint16) []byte {
  136. b := make([]byte, 2)
  137. binary.BigEndian.PutUint16(b, i)
  138. return b
  139. }
  140. func BeEncodeInt32(i int32) []byte {
  141. b := make([]byte, 4)
  142. binary.BigEndian.PutUint32(b, uint32(i))
  143. return b
  144. }
  145. func BeEncodeUint32(i uint32) []byte {
  146. b := make([]byte, 4)
  147. binary.BigEndian.PutUint32(b, i)
  148. return b
  149. }
  150. func BeEncodeInt64(i int64) []byte {
  151. b := make([]byte, 8)
  152. binary.BigEndian.PutUint64(b, uint64(i))
  153. return b
  154. }
  155. func BeEncodeUint64(i uint64) []byte {
  156. b := make([]byte, 8)
  157. binary.BigEndian.PutUint64(b, i)
  158. return b
  159. }
  160. func BeEncodeFloat32(f float32) []byte {
  161. bits := math.Float32bits(f)
  162. b := make([]byte, 4)
  163. binary.BigEndian.PutUint32(b, bits)
  164. return b
  165. }
  166. func BeEncodeFloat64(f float64) []byte {
  167. bits := math.Float64bits(f)
  168. b := make([]byte, 8)
  169. binary.BigEndian.PutUint64(b, bits)
  170. return b
  171. }
  172. // 将二进制解析为int类型,根据[]byte的长度进行自动转换.
  173. // 注意内部使用的是uint*,使用int会造成位丢失。
  174. func BeDecodeToInt(b []byte) int {
  175. if len(b) < 2 {
  176. return int(BeDecodeToUint8(b))
  177. } else if len(b) < 3 {
  178. return int(BeDecodeToUint16(b))
  179. } else if len(b) < 5 {
  180. return int(BeDecodeToUint32(b))
  181. } else {
  182. return int(BeDecodeToUint64(b))
  183. }
  184. }
  185. // 将二进制解析为uint类型,根据[]byte的长度进行自动转换
  186. func BeDecodeToUint(b []byte) uint {
  187. if len(b) < 2 {
  188. return uint(BeDecodeToUint8(b))
  189. } else if len(b) < 3 {
  190. return uint(BeDecodeToUint16(b))
  191. } else if len(b) < 5 {
  192. return uint(BeDecodeToUint32(b))
  193. } else {
  194. return uint(BeDecodeToUint64(b))
  195. }
  196. }
  197. // 将二进制解析为bool类型,识别标准是判断二进制中数值是否都为0,或者为空。
  198. func BeDecodeToBool(b []byte) bool {
  199. if len(b) == 0 {
  200. return false
  201. }
  202. if bytes.Compare(b, make([]byte, len(b))) == 0 {
  203. return false
  204. }
  205. return true
  206. }
  207. func BeDecodeToInt8(b []byte) int8 {
  208. return int8(b[0])
  209. }
  210. func BeDecodeToUint8(b []byte) uint8 {
  211. return uint8(b[0])
  212. }
  213. func BeDecodeToInt16(b []byte) int16 {
  214. return int16(binary.BigEndian.Uint16(BeFillUpSize(b, 2)))
  215. }
  216. func BeDecodeToUint16(b []byte) uint16 {
  217. return binary.BigEndian.Uint16(BeFillUpSize(b, 2))
  218. }
  219. func BeDecodeToInt32(b []byte) int32 {
  220. return int32(binary.BigEndian.Uint32(BeFillUpSize(b, 4)))
  221. }
  222. func BeDecodeToUint32(b []byte) uint32 {
  223. return binary.BigEndian.Uint32(BeFillUpSize(b, 4))
  224. }
  225. func BeDecodeToInt64(b []byte) int64 {
  226. return int64(binary.BigEndian.Uint64(BeFillUpSize(b, 8)))
  227. }
  228. func BeDecodeToUint64(b []byte) uint64 {
  229. return binary.BigEndian.Uint64(BeFillUpSize(b, 8))
  230. }
  231. func BeDecodeToFloat32(b []byte) float32 {
  232. return math.Float32frombits(binary.BigEndian.Uint32(BeFillUpSize(b, 4)))
  233. }
  234. func BeDecodeToFloat64(b []byte) float64 {
  235. return math.Float64frombits(binary.BigEndian.Uint64(BeFillUpSize(b, 8)))
  236. }
  237. // BeFillUpSize fills up the bytes <b> to given length <l> using big BigEndian.
  238. //
  239. // Note that it creates a new bytes slice by copying the original one to avoid changing
  240. // the original parameter bytes.
  241. func BeFillUpSize(b []byte, l int) []byte {
  242. if len(b) >= l {
  243. return b[:l]
  244. }
  245. c := make([]byte, l)
  246. copy(c[l-len(b):], b)
  247. return c
  248. }