PDA

View Full Version : decimal to binary converter



greyash99
August 25th, 2006, 02:10 AM
im trying to make a decimal to binary converter. What I cant get it to do is get the decimal to correspond to its value in my binary list.

#! /usr/local/bin/python
import os
if os.environ['USER']:
print 'Hello, '+os.environ['USER']
decimal = ["0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
binary = ["0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111", "10000", "10001", "10010", "10011", "10100"]
hexa = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "10", "11", "12", "13", "14"]
print "select your conversion"
print "1 decimal to binary"
print "2 decimal to hexadecimal"
print "3 binary to decimal"
print "4 binary to hexadecimal"
print "5 hexadecimal to decimal"
print "6 hexadecimal to binary"
conversion = input(">")
if conversion == 1:
number = float(raw_input("what number?")

print binary

ciscosurfer
August 25th, 2006, 02:31 AM
You must have 'bc' installed. Then try this in a terminal
echo 'obase=2; ibase=10; 123' | bcEnjoy! \\:D/

greyash99
August 25th, 2006, 02:42 AM
thanks, but I was doing this because I could, not becaue I seriously needed it. I just wanted to see if I could do it, and got a little stuck :)

ciscosurfer
August 25th, 2006, 02:46 AM
Ok. Sounds good. I was just showing you that 'bc' already incorporates these functions. But good luck with your script!

greyash99
August 25th, 2006, 02:47 AM
Thanks!=D>

dwblas
August 29th, 2006, 02:21 AM
I don't know if this is what you are talking about, but deicmal to binary would be divide decimal by 2, multiply 1X10 for the whole part of the division and add remainder, so something like

.whole, remain = divmod( int_number, 2 )
.binary_number = 1
.for j in range( 0, whole ) :
.....binary_number *= 10
.binary_number += remain

Haven't thought this through though because I'm not sure you are still interested.

chonger
August 29th, 2006, 06:03 AM
Google is your friend.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300

Are you trying to do this by creating a table of int to binary mapping? If so, I don't see why it wouldn't work.