## Binary >> << Decimal

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

### Binary >> << Decimal

I've been bored lately, writing some codes that may be useful at times(e.g., factorial, permutation calc, etc.). Here is a simple program that converts from either binary to decimal or decimal to binary.

Code: Select all
`print "1) Decimal -> Binary\n2) Binary -> Decimal"usersel = int(raw_input("Please enter your selection: "))if usersel == 1:    finbin = ""    numbin = float(raw_input("Enter the base 10 number: "))    numbinb = numbin    while numbin > 1:        if numbin % 2 == 0:            finbin = "0" + finbin            numbin = numbin / 2        else:            finbin = "1" + finbin            numbin = numbin / 2 - 0.5    finbin = "1" + finbin    print int(numbinb),"in binary is",finbinelif usersel == 2:    binnum = raw_input("Enter the base 2 number: ")    findec = int((binnum),2)    print binnum,"in decimal is",findec`
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM

Arrexel
New User

Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Blog: View Blog (0)

### Re: Binary >> << Decimal

This is cool! I might use this for math class...thanks. I will download when I get a chance.

YayPython
Experienced User

Posts: 50
Joined: Sat Jan 31, 2009 11:05 am
Blog: View Blog (0)

### Who is online

Users browsing this forum: No registered users and 0 guests