package main
import (
"encoding/binary"
"fmt"
)
func main() {
// 一个长度为4的byte切片,表示一个负数
bytes := []byte{0xFF, 0xFF, 0xFF, 0xFF}
// 将byte切片转换为int32
num := int32(binary.BigEndian.Uint32(bytes))
fmt.Printf("Byte切片转换为Int32:%d\n", num)
// 创建一个长度为4的byte切片
ibytes := make([]byte, 4)
// 将int32值写入byte切片中
binary.BigEndian.PutUint32(ibytes, uint32(num))
fmt.Printf("Int32写入Byte切片:%#v\n", ibytes)
}
在线运行效果 in https://go.dev/play/