Skip to content

Commit

Permalink
Add functions to add padding to byte slices
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Díaz <[email protected]>
  • Loading branch information
aalda and gdiazlo committed Feb 25, 2019
1 parent 425e1c2 commit 1854a26
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions util/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@ func Uint64AsBytes(i uint64) []byte {
return b
}

func Uint64AsPaddedBytes(i uint64, n int) []byte {
return AddPaddingToBytes(Uint64AsBytes(i), n)
}

func Uint16AsBytes(i uint16) []byte {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, i)
return b
}

func Uint16AsPaddedBytes(i uint16, n int) []byte {
return AddPaddingToBytes(Uint16AsBytes(i), n)
}

func AddPaddingToBytes(b []byte, n int) []byte {
if len(b)/8 >= n {
return b
}
tmp := make([]byte, n, n)
copy(tmp[n-len(b):], b)
return tmp
}

func BytesAsUint64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
Expand Down

0 comments on commit 1854a26

Please sign in to comment.