Created
July 14, 2011 21:42
-
-
Save localshred/1083527 to your computer and use it in GitHub Desktop.
Generate a byte string of bits to show pass fail for a project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
projects = ['f','p','p','f','p','p','f','p','p','f'] | |
bytes = [] | |
projects.each_slice(4) do |chunk| | |
byte = 0 | |
chunk.each{ |v| byte = (byte << 2) | (v == 'p' ? 1 : 2) } | |
byte = byte << ((4 - chunk.size) * 2) if chunk.size < 4 | |
bytes << byte | |
puts '%s %s %s' % [chunk, byte.chr, byte.chr.unpack("B*")] | |
end | |
byte_str = bytes.map{|b| b.chr }.join | |
bit_str = bytes.map{|b| b.chr.unpack("B*") }.join | |
puts byte_str | |
puts bit_str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
["f", "p", "p", "f"] � ["10010110"] | |
["p", "p", "f", "p"] Y ["01011001"] | |
["p", "f"] ` ["01100000"] | |
�Y` | |
100101100101100101100000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment