Golang下byte和int相互转换

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)))
}

推荐阅读更多精彩内容