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

This is a python program that can convert bianry number( binary is a language that computers use to comunicate with other) to a normal digital number and the other way around.

Python, 96 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys, os, time
def binaryToDigital():
    os.system('cls')
    while True:
        print "To go back to the main menu type 'e' and press enter"
        print
        print """Binary numbers consist of only '1's and '0's 
        """
        binary = raw_input('Type in the binary number: ')
        if binary == "e":
            mainmenu()
        else:
            binarylist = list(binary)
            no = len(binarylist)
            no = no -1
            diginum = 0
            i = 0
            while no > -1:
                if binarylist[no] == '1':
                    kj = 2**i
                    diginum = diginum + kj
                no = no -1
                i = i +1 
            print diginum
            raw_input()

def digitalToBinary():
    os.system('cls')
    try:
        while True:    
            print "To go back to the main menu type 'e' and press enter" 
            print
            number = raw_input('Type in the digital number: ')
            if number == "e":
                mainmenu()
            else:
                number = int(number)
                aaa = str(number)
                i = 0
                numlist = []
                while number != 0:
                    numlist.append(number)
                    number = number/2 
                    i = i + 1
                binarynum = []
                i = i-1
                while i !=-1:
                    bd = str(numlist[i])
                    bdl = list(bd)
                    bdn = len(bdl)
                    bdn = bdn - 1
                    bx = bdl[bdn]
                    if bx == '0' or bx == '2' or bx == '4' or bx == '6' or bx == '8':
                        binarynum.append('0')
                    if bx == '1' or bx == '3' or bx == '5' or bx == '7' or bx == '9':
                        binarynum.append('1')
                    i = i - 1
                print ''.join(binarynum)
                raw_input()
    except ValueError:
        print "The input should only numbers"
        digitalToBinary()
        raw_input()
def credit():
    os.system('cls')
    text = """###########################
Author: phinix bss
Date  : 04/06/2006
Vision: Binary-converter 1.0
###########################
    """
    for character in text:
        sys.stdout.write(character);sys.stdout.flush(),time.sleep(.03),
    raw_input()

def mainmenu():
    while True:
        os.system('cls')
        menu = """What du you want to do?

1) Convert digital to binary
2) Convert binary to digital
3) Credits
4) Exit

pick choice [1-4] : """
        choice = raw_input(menu)
        if choice == "1":
            digitalToBinary()
        if choice == "2":
            binaryToDigital()
        if choice == "3":
            credit()
        if choice == "4":
            sys(exit)
mainmenu()

well i know that there are a lot of people who do not undestand binary numbers yet so this is a code for the people who do not have the time to sit down and calculate or to learn binary counting.

1 comment

Dirk Holtwick 17 years, 10 months ago  # | flag

Build in. To convert binary to decimal you may also try this one:

>>> int("101",2)
5

it also converts from other radixes like 16 (hexadecimal):

>>> int("f", 16)
15