Module: RadixEncoding::Encoding::Bits

Included in:
RadixEncoding::Encoding
Defined in:
lib/radix_encoding/encoding/bits.rb

Instance Method Summary collapse

Instance Method Details

#bits_for(bytes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/radix_encoding/encoding/bits.rb', line 8

def bits_for(bytes)
  bytes.
    map do |byte|
      byte.
        # Convert the byte (a number) to a string with its binary form
        to_s(2).
        # Pad the result with leading zeros to represent all the bits
        rjust(8, "0")
    end.
    # Join all the bits strings
    join.
    # Split the string into an array of characters (each is a bit)
    chars
end