aboutsummaryrefslogtreecommitdiff
path: root/ber/bits.go
blob: 3d1ca41487a36278cea4d5eaeb4c012ef7b54338 (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
31
package ber

// BitString
type Bits []bool

func parseBitString(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 UnmarshalBitString(b []byte) Bits {
	return parseBitString(b)
}

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