package main
import (
"bytes"
"encoding/binary"
)
func IntToBytes(n int) []byte {
data := int64(n)
bytebuf := bytes.NewBuffer([]byte{})
binary.Write(bytebuf, binary.BigEndian, data)
return bytebuf.Bytes()
}
func BytesToInt(bys []byte) int {
bytebuff := bytes.NewBuffer(bys)
var data int64
binary.Read(bytebuff, binary.BigEndian, &data)
return int(data)
}
golang int与byte数组之间的转换
于 2022-05-24 21:19:07 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)