github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/encoding/integers.go (about)

     1  package encoding
     2  
     3  import (
     4  	"encoding/binary"
     5  )
     6  
     7  // EncInt64 encodes an int64 as a slice of 8 bytes.
     8  func EncInt64(i int64) (b []byte) {
     9  	b = make([]byte, 8)
    10  	binary.LittleEndian.PutUint64(b, uint64(i))
    11  	return
    12  }
    13  
    14  // DecInt64 decodes a slice of 8 bytes into an int64.
    15  // If len(b) < 8, the slice is padded with zeros.
    16  func DecInt64(b []byte) int64 {
    17  	b2 := make([]byte, 8)
    18  	copy(b2, b)
    19  	return int64(binary.LittleEndian.Uint64(b2))
    20  }
    21  
    22  // EncUint64 encodes a uint64 as a slice of 8 bytes.
    23  func EncUint64(i uint64) (b []byte) {
    24  	b = make([]byte, 8)
    25  	binary.LittleEndian.PutUint64(b, i)
    26  	return
    27  }
    28  
    29  // DecUint64 decodes a slice of 8 bytes into a uint64.
    30  // If len(b) < 8, the slice is padded with zeros.
    31  func DecUint64(b []byte) uint64 {
    32  	b2 := make([]byte, 8)
    33  	copy(b2, b)
    34  	return binary.LittleEndian.Uint64(b2)
    35  }