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

Chris Parsons chris.p at rsons.org
Mon Jun 11 06:32:47 PDT 2012


On 11 Jun 2012, at 14:25, James Coglan wrote:

> n = Math.ceil(Math.log(charset.size) / Math.log(2))
> chunk = bits[0...n]
> return chunk if chunk =~ /^0/ # definitely within range
> value = chunk.to_i(2)
> return (value >= size.size) ? chunk[0..-2] : chunk

Hmm: at first glance it feels like you're wrapping the three indexes greater than your range into indexes for 13-15 by stripping off the last two bits from the chunk. How about just throwing the bits away if you're not in range? Depends how significant the data is, and whether you need to crunch x bits into exactly x/5 characters or not.

Chris

--
Chris Parsons
chris.p at rsons.org
http://twitter.com/chrismdp
http://pa.rsons.org




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120611/1e47606f/attachment-0003.html>


More information about the Chat mailing list