Caddiana Runes
Caddiana Runes

Reputation: 141

Recursively convert Ascii to binary in python

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?

Upvotes: 0

Views: 875

Answers (3)

dawg
dawg

Reputation: 103874

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'

Upvotes: 0

dansalmo
dansalmo

Reputation: 11686

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'

Upvotes: 1

nicholsonjf
nicholsonjf

Reputation: 1001

>>> 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'

Upvotes: 0

Related Questions