# [Python] Binary/Decimal Conversion Calculator

Ok. So. Who doesn't know how to convert on paper or in their head decimal numbers ==» binary number, or vise-versa? Well. I've successfully programmed a conversion calculator for those who just never took the time to learn, for those who don't care to learn, and for those who've got a hard time doing the conversions on paper or in their head. Basically, when you select option 1 you can type in a number and convert it from a decimal number into a binary. And when you select option 2, you do the opposite. This little program comes in handy when you need to know, or just want to know what the equivalent number, whether it be binary or decimal. Here is an example of a decimal number:

``217``

Here is it's binary equivalent:

``11011001``

Please comment on this, and let me know what you guys think of it. Thanks!

``````# -*- coding: cp1252 -*-
import sys, os, time

#This is where you convert a binary number into a decimal number.
def binaryToDecimal():
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":
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()

#This is where you convert a decimal number into a binary number.
def decimalToBinary():
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":
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()

#This is the credits function....
def credit():
os.system('cls')
text = """###########################
Author: Christopher "Kipp" Moore
Date  : 12/09/2009
Vision: Binary Conversions v1.0
###########################
"""
for character in text:
sys.stdout.write(character);sys.stdout.flush(),time.sleep(.03),
raw_input()

while True:
os.system('cls')
#     What Do You Want To Do?                  #
#                                                                         #
#1) Convert Decimal ==Â» Binary                  #
#2) Convert Binary ==Â» Decimal                 #
#3) Credits                                                       #
#4) Exit Program                                            #
##############################

Choice [1-4] : """
if choice == "1":
decimalToBinary()
if choice == "2":
binaryToDecimal()
if choice == "3":
credit()
if choice == "4":
print "Good-bye! Thanks for using the converter!"
return

vegaseat 1,735

Just a note, there is related Python snippet at:
http://www.daniweb.com/code/snippet216539.html

Also Python has a few built-in functions to offer ...

``````# with Python2 you can convert a
# binary (base 2) string to a denary (base 10) integer
print( int('11111111', 2) )  # --> 255

# Python3 adds the function bin() to convert a
# denary integer (base 10)) to a binary (base 2) object
print( bin(255) )            # --> 0b11111111

# convert back again (Python3 only)
print( int(0b11111111) )     # --> 255``````

]

Thanks for that, vegaseat. Now I know, I wasn't too sure if it was posted on here or not, so I posted it anyways. ;) And, also, thanks for that code snippet you posted as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.