3

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()
2
  • 1
    Well, what's the problem? (Consider showing your output, and your expected output) Commented Feb 14, 2012 at 1:57
  • won't split the input, gives me a mess of chars. Commented Feb 14, 2012 at 2:09

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

okay thank you, now i'm starting to see how to set it up, thank you very much!
Okay this worked very well, thank you very much. everyone will be seeing a lot more of me, i'm very new to python.
2
>>> re.findall('[01]{8}', '010000010100001001000011')
['01000001', '01000010', '01000011']
>>> ''.join(chr(int(x, 2)) for x in re.findall('[01]{8}', '010000010100001001000011'))
'ABC'

2 Comments

thanks, you did it all in 4 lines that's cool, i'll look up more about this 'findall' seems useful.
@user1208098: One line, actually.
2

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.

4 Comments

It's an integer division operator, no need to remember?
@tito, look at the numbers at the bottom. Do they look right?
What are the offsets you're slicing at?
@tito he means to tell you that you are slicing s[0:8], s[1:9], s[2:10] etc. when you should be slicing s[0:8], s[8:16], s[16:24] etc
0

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).

1 Comment

all right except i don't want to join them at the end. what i'd like to do is have them separate, and be able to get them into ascii then to english letters. does that clear it up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.