473,289 Members | 1,756 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,289 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 10037
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.