aboutsummaryrefslogtreecommitdiff
path: root/ber/bits.go
diff options
context:
space:
mode:
Diffstat (limited to 'ber/bits.go')
-rw-r--r--ber/bits.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/ber/bits.go b/ber/bits.go
index 8895e92..180228d 100644
--- a/ber/bits.go
+++ b/ber/bits.go
@@ -15,6 +15,24 @@ func UnmarshalBitString(b []byte) (bs Bits) {
return
}
+func MarshalBitString(bs Bits) (b []byte) {
+ padding := (8 - len(bs)%8) % 8
+ length := len(bs) / 8
+ if padding != 0 {
+ length++
+ }
+ b = make([]byte, length+1)
+ b[0] = byte(padding)
+ for i, bi := range bs {
+ x := 1 + i/8
+ y := 7 - uint(i%8)
+ if bi {
+ b[x] |= 1 << y
+ }
+ }
+ return
+}
+
func (bs Bits) String() (s string) {
bmap := map[bool]string{
true: "1",