user1208098
user1208098

Reputation:

Python: Need help splitting an input of binary code, with no spaces

need to split every 8 char so it will become a list whereby i can then translate into ascii and then english. I just am at a loss for how to split the input,(one large string of binary numbers) into readable binary numbers, instead of just one string.

For example, the input string "010000010100001001000011" can be split in to octets as follows: "01000001","01000010","01000011".

What i have so far:

def main():
    import string

    #take user input of binary
    code = raw_input ('Please type in your binary code to be decoded: ')

    #split the code
    for word in code:
         print code[0::8] + ' '

    #replace the input with the variables
    ascii = ' '
    for word in code:
        ascii = ascii + int(word,2)

    english = ' '
    for word in acsii:
        english = english + chr(word)

    #print the variables to the user
    print english

#call Main
main()

Upvotes: 3

Views: 5677

Answers (4)

Escualo
Escualo

Reputation: 42092

I am not sure I understand your question, but it seems that you are trying to do the following: Given an string of length 8n, convert each chunk of 8 binary digits into a (unicode) string then join the resulting string with no spaces.

If this is the case, then this will do the trick:

stream = "010000010100001001000011"
grouped = [stream[n:n+8] for n in range(len(stream)/8)]
characters = [unichr(int(c, 2)) for c in grouped]
result = u"".join(characters)
# returns u'A\x82\x05'

Edit: You mention "I want them in ASCII and then in English letters", then do the following:

ascii = [int(c, 2) for c in grouped] # this is a list of decimal ascii codes
english = [char(a) for a in ascii] # this is a list of characters - NOT UNICODE

but be careful, chr is only valid in range(256).

Upvotes: 0

tito
tito

Reputation: 13271

I'm not sure that you want this, you maybe it worth it:

>>> s = "010000010100001001000011"
>>> [int(s[i:i+8], 2) for i in xrange(0, len(s), 8)]
[65, 66, 67]

But if you just want the '01' format:

>>> s = "010000010100001001000011"
>>> [s[i:i+8] for i in xrange(0, len(s), 8)]
['01000001', '01000010', '01000011']

thanks Ignacio, i must sleep.

Upvotes: 2

wim
wim

Reputation: 362847

This should help you some of the way, with list comprehensions:

>>> b = '010000010100001001000011'
>>> bin_chunks = [b[8*i:8*(i+1)] for i in xrange(len(b)//8)]
>>> print bin_chunks
['01000001', '01000010', '01000011']
>>> ints = [int(x, 2) for x in bin_chunks]
>>> print ints
[65, 66, 67]
>>> chars = [chr(x) for x in ints]
>>> print chars
['A', 'B', 'C']
>>> print ''.join(chars)
ABC

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

>>> re.findall('[01]{8}', '010000010100001001000011')
['01000001', '01000010', '01000011']
>>> ''.join(chr(int(x, 2)) for x in re.findall('[01]{8}', '010000010100001001000011'))
'ABC'

Upvotes: 2

Related Questions