package main
import (
"bytes"
"encoding/binary"
"fmt"
)
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)
}
func main() {
fmt.Println(IntToBytes(1))
fmt.Println(BytesToInt(IntToBytes(1)))
}
有疑问加站长微信联系(非本文作者)