Is there any neat trick to slice a binary number into groups of five digits in python?
'00010100011011101101110100010111' => ['00010', '00110', '10111', ... ]
Edit: I want to write a cipher/encoder in order to generate "easy to read over the phone" tokens. The standard base32 encoding has the following disadvantages:
- Potential to generate accidental f*words
- Uses confusing chars like chars like 'I', 'L', 'O' (may be confused with 0 and 1)
- Easy to guess sequences ("AAAA", "AAAB", ...)
I was able to roll my own in 20 lines of python, thanks everybody. My encoder leaves off 'I', 'L', 'O' and 'U', and the resulting sequences are hard to guess.
Another way to group iterables, from the itertools examples:
or:
[EDIT]
The question was raised by Greg Hewgill, what to do with the two trailing bits? Here are some possibilities:
How about using a regular expression?
This will break though if your input string contains newlines, that you want in the grouping.
My question was duplicated by this one, so I would answer it here.
I got a more general and memory efficient answer for all this kinds of questions using Generators
So for this question, We can do: