Welcome, guest | Sign In | My Account | Store | Cart

I am new to python so I need lots of help on this code basically what it does is takes a decimal number(155) converts it into binary(10011011) takes the first six numbers in that(100110) converts that into a decimal number(38) and from there makes it a letter("m") according to the base64 code (http://email.about.com/cs/standards/a/base64_encoding.htm)

Python, 46 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#conversion functions
def Binary2Decimal(bin_num):
    """ Return the decimal representation of bin_num
    
        This actually uses the built-in int() function, 
        but is wrapped in a function for consistency """
    return int(bin_num, 2)


def Decimal2Binary(dec_num):
    """ Return the binary representation of dec_num """
    if dec_num == 0: return ''
    head, tail = divmod(dec_num, 2)
    return Decimal2Binary(head) + str(tail)

#input code: converts input into string
s= raw_input("Give Numbers:")
n=0
st=[]
while n<>len(s):
    st.append(s[n])
    n=n+1

#decoding
one = int("".join(st[:3]))
two = int("".join(st[3:6]))
three = int("".join(st[6:9]))
#Decimal two Binary
x = Decimal2Binary(one)
y = Decimal2Binary(two)
z = Decimal2Binary(three)
#Add the Binary together
a = (x+""+y+""+z)
#get additional numbers from Binary
b = a[:6]
c = Binary2Decimal(b)
d = a[6:12]
e = Binary2Decimal(d)
f = a[12:18]
g = Binary2Decimal(f)
h = a[18:24]
i = Binary2Decimal(h)
print c
print e
print g
print i

TEST: type in 155162233 (which is 155 162 233 but if you put spaces it messes up the code) you should get 38, 58, 11, 41 HELP NEEDED ON CODE - I need help with taking out spaces if somebody types them into the input field, because that messes up the string numbering - the code needs to be able to change depending on how many numbers are put in, for example right now if you put more than 9 digits in the code it won't process them (check out the "#get additional numbers from Binary" part of the code, it only processes and prints 4 numbers) - something that converts the output numbers (like 38, 58, 11, 41) into letters (like m, 6, L, p) according to the base64 code My base64 encryption ideas came from http://email.about.com/cs/standards/a/base64_encoding.htm so if you don't get base64, check that site out

PLEASE Email(phillipmates66@hotmail.com me with any edits, suggestions, or other interesting ideas thanks

2 comments

Andreas Kloss 16 years, 5 months ago  # | flag

Have you tried the base64 module?

&gt;&gt;&gt; import base64
&gt;&gt;&gt; base64.encodestring("Test")
'VGVzdA==\n'
&gt;&gt;&gt;
Bob Ippolito 16 years, 5 months ago  # | flag

Who needs a module?

>>> 'Test'.encode('base64')
'VGVzdA==\n'
Created by phillip on Thu, 16 Jun 2005 (PSF)
Python recipes (4591)
phillip's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks