aboutsummaryrefslogtreecommitdiff
path: root/ber/bits.go
blob: 8895e926c536e9e273ee38082d12dd1801e266d0 (plain)
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
28
29
30
package ber

// BitString
type Bits []bool

func UnmarshalBitString(b []byte) (bs Bits) {
	padding := int(b[0])
	length := (len(b)-1)*8 - padding
	for i := 0; i < length; i++ {
		x := 1 + i/8
		y := 7 - uint(i%8)
		bit := b[x]&(1<<y) != 0
		bs = append(bs, bit)
	}
	return
}

func (bs Bits) String() (s string) {
	bmap := map[bool]string{
		true:  "1",
		false: "0",
	}
	for i, bit := range bs {
		if i > 0 && i%4 == 0 {
			s += " "
		}
		s += bmap[bit]
	}
	return
}