Skip to content

Instantly share code, notes, and snippets.

@tqcenglish
Created June 11, 2020 01:20
Show Gist options
  • Save tqcenglish/61c5eebe26fc04425f39cd10838f29bc to your computer and use it in GitHub Desktop.
Save tqcenglish/61c5eebe26fc04425f39cd10838f29bc to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
//IntToBytes 256 => [0 0 0 0 0 0 1 0]
func IntToBytes(n int) []byte {
data := int64(n)
bytebuf := bytes.NewBuffer([]byte{})
binary.Write(bytebuf, binary.BigEndian, data)
return bytebuf.Bytes()
}
//BytesToInt [0 0 0 0 0 0 1 0] => 256
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(256))
fmt.Println(BytesToInt([]byte{0, 0, 0, 0, 0, 0, 1, 0}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment