473,390 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 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 10039
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

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: Michael A. Covington | last post by:
Microsoft's documentation is a bit unclear, but is Decimal in fact a radix-100 (base-100) arithmetic data type (in which each byte ranges only from 0 to 99)? It should be straightforward to dump...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
1
by: lmh86 | last post by:
Should the following algorithm produce the binary equivalent of an inputed decimal value? ---------- cout << "Decimal value: "; cin >> Decimal; Binary = 0; Remainder = Decimal; for(Power =...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: Tukeind | last post by:
Hello, I am receiving the following error: error C2065: 'to_binary' : undeclared identifier while running the code below. If anyone can help I'll appreciate it? Thank you, Tukeind
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
0
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.