mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 00:39:26 +00:00
Added byte helpers
This commit is contained in:
parent
ebef4e103b
commit
6ab61f2c52
1 changed files with 27 additions and 0 deletions
27
bytes.go
Normal file
27
bytes.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue