Skip to content

Instantly share code, notes, and snippets.

@localshred
Created July 14, 2011 21:42
Show Gist options
  • Save localshred/1083527 to your computer and use it in GitHub Desktop.
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
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
["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