0

I have been working on a Python project where I am trying to use the original ASCII code, and incorporate it into a Cipher. But before that, I was wondering if there was a way to recursively convert an ascii to binary. Python 2.7.5 btw. Here's my code:

inputMessage= raw_input("Enter message for conversion: ")
        print "Decoded string: "
        #text to ascii
        for ch in inputMessage:
            print ord(ch)
        print "\n\n"
        #ascii to binary
        print "ASCII to Binary:"
        print bin(ord(ch))
        print
        print "The Binary without the '0b':"
        for ch in inputMessage:
            print bin(ord(ch))[2::]

my output in the shell:

Enter message for conversion: Hi, my name is Johnny
Decoded string: 
72
105
44
32
109
121
32
110
97
109
101
32
105
115
32
74
111
104
110
110
121

ASCII to Binary:
0b1001000
0b1101001
0b101100
0b100000
0b1101101
0b1111001
0b100000
0b1101110
0b1100001
0b1101101
0b1100101
0b100000
0b1101001
0b1110011
0b100000
0b1001010
0b1101111
0b1101000
0b1101110
0b1101110
0b1111001

what I need to find out is how can I recursively run through each number of the ascii output of number when converting to binary. Anyone have a solution?

1

3 Answers 3

1
def toBin(n):
    return toBin(n/2) + str(n%2) if n else ""

>>> toBin(ord('A'))
'1000001'

>>> Message= raw_input("Enter message for conversion: ")
Enter message for conversion: Hey now!

>>> ''.join(toBin(ord(c)) for c in Message)
'100100011001011111001100000110111011011111110111100001'

Use this if you need it to work for n=0

def toBin(n, z=1):
    return toBin(n/2, 0) + str(n%2) if n else str(n)*z

If you need to pad to 8 bits you can do:

def toBin(n, z=8):
   return toBin(n/2, z-1) + str(n%2) if n else str(n)*z

>>> toBin(10)
'00001010'
>>> toBin(ord("A"))
'01000001'
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that this returns '' for the legit value of toBin(0) If you change return toBin(n/2) + str(n%2) if n else "" to return toBin(n/2) + str(n%2) if n else "0" then you fix that but get a leading 0 on all bin values. Hmmm.
0 is not an ascii value as needed by the problem.
0
>>> my_string = 'myname is james'
>>> my_binary = [bin(ord(i)) for i in js]
>>> my_binary
['0b1101101', '0b1111001', '0b1101110', '0b1100001', '0b1101101', '0b1100101', '0b100000', '0b1101001', '0b1110011', '0b100000', '0b1101010', '0b1100001', '0b1101101', '0b1100101', '0b1110011']
>>> bin = ''.join(jb)
>>> bin
'0b11011010b11110010b11011100b11000010b11011010b11001010b1000000b11010010b11100110b1000000b11010100b11000010b11011010b11001010b1110011'

2 Comments

thank you. This makes sense now that I can just see the process. :D
you need to remove 0b prefix and pad with zeros if necessary (one byte - 8 bits).
0

Here is a recursive conversion of decimal base 10 to binary list of digits:

def binary(n):
   if n < 2:
       return [n]
   else:
       return binary(n / 2) + [n % 2]

Use it like so:

>>> tgt='33'
>>> print ''.join(map(str, binary(int(tgt))))
100001

If you want to return strings directly (bypassing a list and string conversion) you can use this function:

def d2bin(n):
    s=str(n%2)
    if n>>1 > 0:
        s+=d2bin(n>>1)
    return s

Use like so:

>>> d2bin(0)
'0'
>>> d2bin(65)
'1000001'

1 Comment

It seems OP wants: 'ab' -> 0110000101100010 i.e., '33' -> 0011001100110011

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.