[LRUG] How would you turn a bit stream into a character stream?

James Coglan jcoglan at gmail.com
Mon Jun 11 05:56:31 PDT 2012


Hi all,

I have a problem I'm not sure how to solve, more of a general programming
problem than a Ruby thing. Basically, say you have an indefinite stream of
random bits:

1100111001110110101100000110101011011110110011111001101000001000111000100000011001111110100010000001000001111110100010010100111111011101110110011111001101010101101111111011101111111000010001001001100100100101011100001001000001110101011101101101010011100111
...

and you want to turn it into a stream of characters from an arbitrary-size
list of characters, let's say the 26 letters from A to Z. You want the
letters to be evenly distributed in the output; each letter should have an
equal probability of appearing.

This is trivial if the character set's size is a power of two, since you
can chop off log2(N) bits at a time and turn them into a valid index. But
I'm not sure how to do it correctly for arbitrary set sizes. Suggestions?

-- 
James Coglan
http://jcoglan.com
+44 (0) 7771512510
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120611/5a36b430/attachment-0003.html>


More information about the Chat mailing list