This is a creation in Article, where the information may have evolved or changed.
When using Golang for data transmission, you will often encounter the mutual transfer of byte and int, but Golang does not have a ready-made method, so it can only be solved by binary package.
So, need: Import "encoding/binary", and because it is a byte conversion, so also involves Bytes:import "bytes"
The code is as follows:
Package Main
Import (
"Bytes"
"Encoding/binary"
"FMT"
"Strings"
)
Func Main () {
b : = []byte{0x00, 0x00, 0x03, 0xe8}
B_buf : = bytes . Newbuffer (b)
var x int32
Binary. Read (b_buf, Binary. Bigendian, &x)
Fmt. PRINTLN (x)
Fmt. PRINTLN (Strings. Repeat ("-", 100))
x =
b_buf = bytes . Newbuffer ([]byte{})
Binary. Write (b_buf, Binary. Bigendian, X)
Fmt. Println (b_buf. Bytes ())
}
This is basically the solution. In fact, the following does not assign value x also does not matter. Because it's already.