diff options
author | obscuren <obscuren@obscura.com> | 2014-01-04 07:32:13 +0800 |
---|---|---|
committer | obscuren <obscuren@obscura.com> | 2014-01-04 07:32:13 +0800 |
commit | 6ab61f2c524686e479f9546d5ce2529f3b8eb7fe (patch) | |
tree | 0b58a91f717148401f1a751c4570f29703082774 | |
parent | ebef4e103b4601e477a787ace068dbf181acc39c (diff) | |
download | go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.tar go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.tar.gz go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.tar.bz2 go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.tar.lz go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.tar.xz go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.tar.zst go-tangerine-6ab61f2c524686e479f9546d5ce2529f3b8eb7fe.zip |
Added byte helpers
-rw-r--r-- | bytes.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/bytes.go b/bytes.go new file mode 100644 index 000000000..6bf381343 --- /dev/null +++ b/bytes.go @@ -0,0 +1,27 @@ +package main + +import ( + "bytes" + "encoding/binary" + "fmt" +) + +func NumberToBytes(num uint64, bits int) []byte { + buf := new(bytes.Buffer) + err := binary.Write(buf, binary.BigEndian, num) + if err != nil { + fmt.Println("binary.Write failed:", err) + } + + return buf.Bytes()[buf.Len()-(bits / 8):] +} + +func BytesToNumber(b []byte) (number uint64) { + buf := bytes.NewReader(b) + err := binary.Read(buf, binary.LittleEndian, &number) + if err != nil { + fmt.Println("binary.Read failed:", err) + } + + return +} |