471,349 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Decimal to binary

I'm new to Python I'm trying to create a program that converts decimal to binary but am having trouble.

I understand the arithmetics of it, but I was wondering how I can add the remainders onto each other using strings. So for example, the binary of 6 would be 110. The first remainder computed would be 0, 1, and then finally 1. How do I put those three numbers together to create 110 as a string. I apologize but I hope this is a bit comprehensible.
Oct 18 '07 #1
8 9813
rhitam30111985
112 100+
I'm new to Python I'm trying to create a program that converts decimal to binary but am having trouble.

I understand the arithmetics of it, but I was wondering how I can add the remainders onto each other using strings. So for example, the binary of 6 would be 110. The first remainder computed would be 0, 1, and then finally 1. How do I put those three numbers together to create 110 as a string. I apologize but I hope this is a bit comprehensible.
keep storing the remainder in a list by using the list.append() function.. then reverse the list using list.reverse() fuction.... then join it using the join fuction ''.join(list).. and u have the binary number in the string form... now use type casting to make it an integer : int(string)

thats it
Oct 18 '07 #2
keep storing the remainder in a list by using the list.append() function.. then reverse the list using list.reverse() fuction.... then join it using the join fuction ''.join(list).. and u have the binary number in the string form... now use type casting to make it an integer : int(string)

thats it
Is there any way to do it without using list?
Oct 18 '07 #3
rhitam30111985
112 100+
yeah there is .. but what have u tried so far ? post your code ...
Oct 18 '07 #4
Expand|Select|Wrap|Line Numbers
  1. n=int(raw_input("number: "))
  2. b=[]
  3. while n>0:
  4.     q=n/2 
  5.     r=n%2  
  6.     b.append(r)
  7.     if q!=0:
  8.         q=n
  9.     print b
  10.  
  11.  
I'm trying list but it won't work. How do I store remainders?
Oct 18 '07 #5
rhitam30111985
112 100+
Expand|Select|Wrap|Line Numbers
  1. n=int(raw_input("number: "))
  2. b=[]
  3. while n>0:
  4.     q=n/2 
  5.     r=n%2  
  6.     b.append(r)
  7.     if q!=0:
  8.         q=n
  9.     print b
  10.  
  11.  
I'm trying list but it won't work. How do I store remainders?
this looks like an infinite loop ..

(while n>0)

n is always greater than 0. since u r not modifying its value anywhere in ur code.. anyway. using list u can do it lie this:

Expand|Select|Wrap|Line Numbers
  1. a=22
  2. b=[]
  3. while a:
  4.      b.append(a%2)
  5.      a/=2
  6. b.reverse()
  7.  
without using list:
Expand|Select|Wrap|Line Numbers
  1. def binary(number):
  2.      bin=0
  3.      while number:
  4.              remainder= number % 2
  5.              bin+=remainder
  6.              bin*=10
  7.              number /=2
  8.      while bin:
  9.              number+=bin%10
  10.              bin/=10
  11.              number*=10
  12.      return number
  13.  
Oct 18 '07 #6
bartonc
6,596 Expert 4TB
Thoroughly discussed here.
Oct 18 '07 #7
Hi there again. I'm trying to convert a decimal number into binary using two's complement. Is there a function that'll allow me to flip the integers around?
Oct 22 '07 #8
bartonc
6,596 Expert 4TB
Thoroughly discussed here.
It's close enough to the original topic that I have merge your two threads.
Oct 22 '07 #9

Post your reply

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

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
5 posts views Thread by Michael A. Covington | last post: by
687 posts views Thread by cody | last post: by
2 posts views Thread by Steve Summit | last post: by
1 post views Thread by lmh86 | last post: by
3 posts views Thread by zgfareed | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.