I have very long binary numbers stored in strings. Each 8 characters (i.e. each 8-bit chunk) represent an ASCII character code. To give an example, 0100100001101001 is 2 8-bit numbers (01001000 & 01101001), which are the character codes for 'H' & 'i'. So the whole thing is a binary representation of 'Hi'.
My question is, is there a way to force a for loop iterate through a string in bigger chunks so that I can read 8 letters at a time? In other words, I'd like the for loop to assign 8 characters to my iterator variable per loop iteration instead of 1, so that I can easily determine the character codes represented by the string.
Thanks in advance.
Just throwing this answer in, perhaps not the most appropriate way to do things, but you can use textwrap:
>>> import textwrap
>>> s = '0100100001101001'
>>> textwrap.wrap(s, 8)
['01001000', '01101001']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With