# Thread: Decimal to binary converter help!

## Decimal to binary converter help!

Ok i'm doing a decimal to binary converter with C# i know most of you don't know the language but it's very similar to C++ so please take a look

Code:
```/*The equation to convert Decimal to Binary is the following:
* *)Take the number in a variable called x
* 2)Get the remainder of x untill it's equal to * and put them in an array list
* *)Print the array backwards
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace NS
{
class Program
{
static void Main(string[] args)
{
// Take the number we would like to convert ....
int x = 0;
//The remainder will be here...
ArrayList arr = new ArrayList();
//End of variable declaration.....
Console.Write("Enter any number you would like to convert: ");
//Got the number now let's divide it untill it's equal 0 and pickup the remainders
for (int i = 0; x > 0; i++)
{
for (int s = 0; i < arr.Count; s++)
{
Console.WriteLine(arr[s]);
}
}
}
}
}```
I want the program to get the remainder of x untill x is equal to 0 and print the remainders :-) using an arraylist
** an arraylist is an object variable with no limits ... you can make an arraylist of integers and strings at the same time! So i'm using an arraylist to get all the remainders and print them backwards later (to convert to binary)
Also visit : [URL="http://www.helpwithpcs.com/courses/binary-numbers.htm"]http://www.helpwithpcs.com/courses/binary-numbers.htm[/URL]
to be able to help me , thanks!

## Found something!

Ok i found the error in the code while deb***ing! i spoted that x stayed constant because i haven't done anything to it! i just got it's remainders! so that will create an infite loop! so i did that inside the for loop
Code:
```for (int i = 0; x > 0; i++)
{
x /= 2;
Console.Write(arr[i]);
}```
but there's still something wrong!! the binary number written isn't right!! :S
yes i know i need to reverse it! but when you do that manualy it's still wrong!
try the number *57 it should be : *00***0* but when not reversed it will be: *0***00* ... but the program's result is way diffrent :S

## Yayy!!

Ok fixed the second error :-) but i still need to reverse the order of the array list!
here's the fix:

Code:
```for (int i = 0; x > 0; i++)
{
y = x % 2;
x /= 2;
Console.Write(arr[i]);
}```
i found out that it divides X then get the remainder of the divided number!
for example : if i enter *57! it would do *57 / 2 = 78 and get the remainder of 78!! so i just put the remainder before the division!

ok now to reverse the order of the whole program! i found arr.Reverse() but i don't know how to use it xD any help?

## OMG loool i fixed it!

It's kind of odd! My mind was blocked untill i asked those questions in this forum xD!! ! anyway i finished the decimal to binary converter here's the whole thing ... works flawlessly!
Code:
```using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace NS
{
class Program
{
static void Main(string[] args)
{
// Take the number we would like to convert ....
int x = 0;
int y = 0;
//The remainder will be here...
ArrayList arr = new ArrayList();
//End of variable declaration.....
Console.Write("Enter any number you would like to convert: ");
//Got the number now let's divide it untill it's equal 0 and pickup the remainders
for (int i = 0; x > 0; i++)
{
y = x % 2;
x /= 2;
}
//Got the numbers in the array list now let's reorder them :-)
arr.Reverse();
//reordered the list now let's print the numbers in the arraylist!
for (int s = 0; s < arr.Count; s++)
{
Console.Write(arr[s]);
}
Console.WriteLine();
}
}
}```
Lool tought it would be easier though!ENJOY!
i'm going to do a text to binary converter later on xD!

5. Registered User
Join Date
Dec 2009
Posts
6

## Decimal to binary converter help

I am very thankful to ur reply but i actually i wan to compare one sound file to another in a some programming language. so if u have any idea please help me.
Another thing i wanted to ask is whether the code given by the binary converter is the actual way in which the sound file is stored in our system with the addresses same as given in the file in the converter?

## You got the wrong idea i think

Look i'm not a real expert in binary convertions! And the converter i made above is a simple converter that could convert simple decimal numbers you put into binary for example : 5 it would convert it to *0* in binary!

7. Registered User
Join Date
Jan 2010
Posts
1
In arithmetic, the radix or base refers to the number b in an expression of the form bn.[URL="http://www.testking.net/testking-*52-00*.htm"]*52-00*[/URL] The number n is called the exponent and the expression is known formally as exponentiation of b by n or the exponential of n with base b. It is more commonly expressed as "the nth power of b",[URL="http://www.testking.net/testking-JN0-*50.htm"]JN0-*50[/URL] "b to the nth power" or "b to the power n". The term power strictly refers to the entire expression, but is sometimes used to refer to the exponent.When b is an integer bigger than *,[URL="http://www.testking.net/testking-XK0-002.htm"]XK0-002[/URL]
this process is particularly important in positional numeral systems for denoting numbers. For a given integer b the positional numeral syste

8. Registered User
Join Date
Jan 2010
Posts
1
The decimal (base ten) numeral system has ten possible values (0,*,2,*,4,5,6,7,8, or *) for each place-value. In contrast, the binary (base two) numeral system has two possible values, often represented as 0 or *, for each place-value.

To avoid confusion while using different numeral systems, the base of each individual number may be specified by writing it as a subscript of the number. For example, the decimal number *56 may be written as *56*0 and read as "one hundred fifty-six, base ten". The binary number *00***00 may be specified as "base two" by writing it as *00***002.

Since the binary system is the internal language of electronic computers, serious computer programmers should understand how to convert from decimal to binary. Although, converting in the opposite direction, from binary to decimal, is often easier to learn first.
________________________________________
[url=http://www.housepaintermassachusetts.com]house painter massachsuetts[/url]
[url=http://www.scottsdalelenderforeclosure.com]Scottsdale Foreclosures[/url]

9. Registered User
Join Date
Jan 2010
Posts
1
Hi guys,
I'm sure this tool can help, I use it to make sure I'm converting the right way
Use this [URL="http://www.stringfunction.com/decimal-binary.html"]decimal to binary converter[/URL]
David

10. Registered User
Join Date
Jan 2010
Posts
3
Your problem is in the for. You're accessing indexes that haven't been given a value, and considering that an int that hasn't been given a value starts with a unpredictable number, it is printing that number.
Change the for to:
for( ; k>0 ; )
printf("%d", b[--k]);
Last edited by gordo; 01-25-2010 at 07:24 AM.