473,480 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Decimal to binary

4 New Member
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 10044
rhitam30111985
112 New Member
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
Dreadknought
4 New Member
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 New Member
yeah there is .. but what have u tried so far ? post your code ...
Oct 18 '07 #4
Dreadknought
4 New Member
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 New Member
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 Recognized Expert Expert
Thoroughly discussed here.
Oct 18 '07 #7
Dreadknought
4 New Member
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 Recognized Expert Expert
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
4485
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
6097
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
1959
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
22779
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
3443
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
1633
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
5794
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
4480
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
8828
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
28036
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
7034
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
7076
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
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.