Return n number of bits into a byte array
(n uint)
| 126 | |
| 127 | // Return n number of bits into a byte array |
| 128 | func (p *BitReader) ReadBitsToByteArray(n uint) ([]byte, error) { |
| 129 | result := make([]byte, int(math.Ceil(float64(n)/8))) |
| 130 | |
| 131 | temp := make([]byte, n) |
| 132 | for i := uint(0); i < n; i++ { |
| 133 | bit, err := p.ReadBit() |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | temp[i] = bit |
| 138 | } |
| 139 | |
| 140 | bitmask := 0 |
| 141 | for i := n; i > 0; i-- { |
| 142 | index := len(result) - (bitmask / 8) - 1 |
| 143 | result[index] |= temp[i-1] << uint(bitmask%8) |
| 144 | bitmask++ |
| 145 | } |
| 146 | |
| 147 | return result, nil |
| 148 | } |
| 149 | |
| 150 | // Return n number of bits |
| 151 | func (p *BitReader) ReadBits(n uint) (byte, error) { |