Created
June 11, 2020 01:20
-
-
Save tqcenglish/61c5eebe26fc04425f39cd10838f29bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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