aboutsummaryrefslogtreecommitdiffstats
path: root/bytes.go
blob: 6bf38134301fa0ee28e085338a4001c6037770b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
}