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?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Caddiana Runes
  • 121
  • 1
  • 2
  • 5
  • related: [Convert Binary to ASCII and vice versa (Python)](http://stackoverflow.com/q/7396849/4279) – jfs Dec 13 '13 at 02:43

3 Answers3

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'
dansalmo
  • 11,506
  • 5
  • 58
  • 53
  • 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. – dawg Dec 13 '13 at 01:33
  • 0 is not an ascii value as needed by the problem. – dansalmo Dec 13 '13 at 01:34
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'
nicholsonjf
  • 971
  • 2
  • 11
  • 21
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'
dawg
  • 98,345
  • 23
  • 131
  • 206
  • It seems OP wants: `'ab'` -> `0110000101100010` i.e., `'33'` -> `0011001100110011` – jfs Dec 13 '13 at 02:40