github.com/ZuluSpl0it/Sia@v1.3.7/encoding/integers.go (about) 1 package encoding 2 3 import ( 4 "encoding/binary" 5 "io" 6 ) 7 8 // EncInt64 encodes an int64 as a slice of 8 bytes. 9 func EncInt64(i int64) (b []byte) { 10 b = make([]byte, 8) 11 binary.LittleEndian.PutUint64(b, uint64(i)) 12 return 13 } 14 15 // DecInt64 decodes a slice of 8 bytes into an int64. 16 // If len(b) < 8, the slice is padded with zeros. 17 func DecInt64(b []byte) int64 { 18 b2 := make([]byte, 8) 19 copy(b2, b) 20 return int64(binary.LittleEndian.Uint64(b2)) 21 } 22 23 // EncUint64 encodes a uint64 as a slice of 8 bytes. 24 func EncUint64(i uint64) (b []byte) { 25 b = make([]byte, 8) 26 binary.LittleEndian.PutUint64(b, i) 27 return 28 } 29 30 // DecUint64 decodes a slice of 8 bytes into a uint64. 31 // If len(b) < 8, the slice is padded with zeros. 32 func DecUint64(b []byte) uint64 { 33 b2 := make([]byte, 8) 34 copy(b2, b) 35 return binary.LittleEndian.Uint64(b2) 36 } 37 38 // WriteUint64 writes u to w. 39 func WriteUint64(w io.Writer, u uint64) error { 40 _, err := w.Write(EncUint64(u)) 41 return err 42 } 43 44 // WriteInt writes i to w. 45 func WriteInt(w io.Writer, i int) error { 46 return WriteUint64(w, uint64(i)) 47 }