Slice a binary number into groups of five digits

2019-02-25 07:00发布

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.

标签: python slice
7条回答
何必那么认真
2楼-- · 2019-02-25 07:48

Per your comments, you actually want base 32 strings.

>>> import base64
>>> base64.b32encode("good stuff")
'M5XW6ZBAON2HKZTG'
查看更多
登录 后发表回答