By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,428 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,428 IT Pros & Developers. It's quick & easy.

need help converting decimal numbers to binary and vice versa

P: 10
like we have an integer value of : 9
and i need to convert it to binary numbers

and vice versa, thanks!
Dec 1 '08 #1
Share this Question
Share on Google+
7 Replies


P: 10
oh wait, how about this..
a list:
[0, 0, 1, 1]

i need to make it so that it will read that as 0011, so that i will be a binary number.

so i need [0, 0, 1, 1] to equal 0011

THEN i need help to convert 0011 into base 10...THANKS
Dec 1 '08 #2

P: 10
well i did this,
Expand|Select|Wrap|Line Numbers
  1. for a in patrow2:
  2.         print "      "
  3.         for b in a:
  4.              print b,
  5.  
OUTPUT:
**
1 0 0
1 1 0
1 1 0
**
patrow2 is the list of lists , which in this case is [[1, 0, 0], [1, 1, 0], [1, 1, 0]]

but how do u get rid of the spaces in between the numbers?
Dec 1 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
@victory2006
Following are two ways:
Expand|Select|Wrap|Line Numbers
  1. >>> patrow2 = [[1, 0, 0], [1, 1, 0], [1, 1, 0]]
  2. >>> for item in patrow2:
  3. ...     print ''.join([str(i) for i in item])
  4. ...     
  5. 100
  6. 110
  7. 110
  8. >>> for item in patrow2:
  9. ...     print '%d%d%d' % tuple(item)
  10. ...     
  11. 100
  12. 110
  13. 110
  14. >>> 
To convert from binary to decimal, use built-in function int().

Expand|Select|Wrap|Line Numbers
  1. >>> int('011', 2)
  2. 3
  3. >>> 
Dec 1 '08 #4

P: 3
Something I whipped up once to convert decimal to binary:

Expand|Select|Wrap|Line Numbers
  1. def dec2biprog():
  2.     empt = ""
  3.     def dec2bi(x):
  4.         y = empt
  5.         z = 0
  6.         power = range(1, 51)
  7.         power.reverse()
  8.         for i in power:
  9.             power[z] = 2**power[z]
  10.             z = z + 1
  11.         power.append(1)
  12.         for i in power:
  13.             if x >= i:
  14.                 x = x - i
  15.                 y = y + "1"
  16.             else:
  17.                 y = y + "0"
  18.         a = 0
  19.         for i in y:
  20.             if i == "0":
  21.                 a = a + 1
  22.             elif i == "1":
  23.                 return y[a:100]
  24.  
And the UI:

Expand|Select|Wrap|Line Numbers
  1.     print """Welcome to D2B, the program that converts your decimal
  2.     (or base 10) numbers into binary (or base 2) numbers.
  3.     """
  4.     erone = 0
  5.     while True:
  6.         inp = raw_input("Please enter your number here: ")
  7.         print empt
  8.         try:
  9.             if inp == "0" or int(inp) < 0:
  10.                 print "Please pick a number greater than 0!"
  11.                 erone = 1
  12.         except ValueError:
  13.             print "Pick a number!"
  14.             erone = 1
  15.         if erone != 1:
  16.             if int(inp) > 1125899906842624:
  17.                 print "Pick a number less than 1,125,899,906,842,625, please."
  18.                 erone = 1
  19.         if erone != 1:
  20.             print "Your number, " + str(inp) + ", is " + dec2bi(int(inp)) + " in binary!"
  21.         else:
  22.             erone = 0
  23.         print empt
  24.         ertest = 0
  25. dec2biprog()
  26.  
Dec 2 '08 #5

bvdet
Expert Mod 2.5K+
P: 2,851
MorseCode,

Thank you for sharing your code with us. You can streamline your code a bit by using range() with a negative step in a list comprehension.
Original code:
Expand|Select|Wrap|Line Numbers
  1.         z = 0
  2.         power = range(1, 51)
  3.         power.reverse()
  4.         for i in power:
  5.             power[z] = 2**power[z]
  6.             z = z + 1
  7.         power.append(1)
Recommended code:
Expand|Select|Wrap|Line Numbers
  1.         power = [2**i for i in range(50, -1, -1)]

You are removing the leftmost "0"s with a for loop. You can use string method lstrip() for this.
Original code:
Expand|Select|Wrap|Line Numbers
  1.  
  2.         a = 0
  3.         for i in y:
  4.             if i == "0":
  5.                 a = a + 1
  6.             elif i == "1":
  7.                 return y[a:100]
Recommended code:
Expand|Select|Wrap|Line Numbers
  1.         return y.lstrip('0')

There is nothing wrong with string concatenation. I prefer string formatting for efficiency and readability.
Original code:
Expand|Select|Wrap|Line Numbers
  1. print "Your number, " + str(inp) + ", is " + dec2bi(int(inp)) + " in binary!"
Recommended code:
Expand|Select|Wrap|Line Numbers
  1. print "Your number, %s, is %s in binary" % (inp, dec2bi(int(inp))
Dec 2 '08 #6

P: 3
Thanks for the help, bv, you taught me (a fairly new newb) a few things I didn't know.
Namely, everything you showed me in your post. Lol.
Dec 2 '08 #7

bvdet
Expert Mod 2.5K+
P: 2,851
You're welcome MorseCode. Following is a function that will convert a decimal number to any base between and including 2-36. It uses built-in function divmod() and recursion.
Expand|Select|Wrap|Line Numbers
  1. def convDecToBase(num, base, dd=False):
  2.     ''' Convert a decimal integer to a base between and including 2-36.'''
  3.     if not 2 <= base <= 36:
  4.         raise ValueError, 'The base number must be between 2 and 36.'
  5.     if not dd:
  6.         dd = dict(zip(range(36), list(string.digits+string.ascii_lowercase)))
  7.     if num == 0: return ''
  8.     num, rem = divmod(num, base)
  9.     return convDecToBase(num, base, dd)+dd[rem]
Dec 2 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.