473,909 Members | 4,189 Online
Bytes | Software Development & Data Engineering Community
+ 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 10070
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
4555
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
6174
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
5
1991
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 some Decimal values onto a binary file (or into a stream that writes into an array of bytes) and examine them.
687
23986
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 believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
2
3475
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
1
1652
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 = 4; Power = -1; Power--) { Remainder = Decimal - (pow(2,Power));
28
5964
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
2
4514
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
8855
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 for numbers 0 to 255. What am doing wrong or not doing with my output? Source code: // code #include <iostream>
0
28101
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 example following this one demonstrates how to convert decimal numbers into binary values. A hex number is a string that contains numbers between 0-9 and characters A-F; where A = 10, B = 11, ..., F = 15. (For more information please look up...
0
10035
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9877
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10919
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11046
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10538
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7248
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5938
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.