We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent eb19fcf commit cbd42adCopy full SHA for cbd42ad
other/tool_test.go
@@ -1,6 +1,7 @@
1
package main
2
3
import (
4
+ "encoding/binary"
5
"fmt"
6
"math/rand"
7
"net"
@@ -119,3 +120,19 @@ func TestAppent(t *testing.T) {
119
120
}
121
fmt.Println("info", info)
122
123
+
124
+func TestBinary(t *testing.T) {
125
+ var info = []byte("123") //要传输数据
126
+ id := make([]byte, 2) //定义传输的类型
127
+ var data = make([]byte, len(info)+2) //定义发送内容
128
+ binary.BigEndian.PutUint16(id, 12) // 将类型转换为二进制
129
+ copy(data[:2], id) // 前2位保存类型
130
+ copy(data[2:], info) //后面保存要传输的内容
131
+ t.Log("id", id)
132
+ t.Log("info", info)
133
+ t.Log("data", data)
134
+ i := binary.BigEndian.Uint16(data[:2]) //将二进制类型转换
135
+ t.Log(i)
136
+ t.Log(data[2:])
137
138
+}
0 commit comments