package mainimport "bytes"import "encoding/binary"import "fmt"import "strings"func main() { b := []byte{0x00,0x00,0x03,0xe8} bin_buf := bytes.NewBuffer(b) var x int32 binary.Read(bin_buf, binary.BigEndian, &x) fmt.Println(x) fmt.Println(strings.Repeat("~", 20)) x = 1000 bin_buf = bytes.NewBuffer([]byte{}) binary.Write(bin_buf, binary.BigEndian, x) fmt.Println(bin_buf.Bytes()) str2 := "hello" data2 := []byte(str2) fmt.Println(data2) str2 = string(data2[:]) fmt.Println(str2)}
编译输出:
C:\Go\bin\go.exe run conver.go1000~~~~~~~~~~~~~~~~~~~~[0 0 3 232][104 101 108 108 111]hello