View Full Version : Decimal to binary converter help!
minaadel1994
12-21-2009, 03:06 AM
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
/*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: ");
x = int.Parse(Console.ReadLine());
//Got the number now let's divide it untill it's equal 0 and pickup the remainders
for (int i = 0; x > 0; i++)
{
arr.Add(x % 2);
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 : http://www.helpwithpcs.com/courses/binary-numbers.htm
to be able to help me , thanks! :)
minaadel1994
12-21-2009, 03:35 AM
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
for (int i = 0; x > 0; i++)
{
x /= 2;
arr.Add(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
minaadel1994
12-21-2009, 03:39 AM
Ok fixed the second error :-) but i still need to reverse the order of the array list!
here's the fix:
for (int i = 0; x > 0; i++)
{
y = x % 2;
x /= 2;
arr.Add(y);
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? :D
minaadel1994
12-21-2009, 04:00 AM
It's kind of odd! My mind was blocked untill i asked those questions in this forum xD!! :) :D ! anyway i finished the decimal to binary converter here's the whole thing ... works flawlessly!
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: ");
x = int.Parse(Console.ReadLine());
//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;
arr.Add(y);
}
//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!
poegreeInputt
12-23-2009, 09:26 AM
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?
minaadel1994
12-25-2009, 11:13 AM
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!
I'm sorry but i can't help you on this one mate :(
mrktlr
01-07-2010, 05:39 AM
In arithmetic, the radix or base refers to the number b in an expression of the form bn.*52-00* (http://www.testking.net/testking-*52-00*.htm) 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",JN0-*50 (http://www.testking.net/testking-JN0-*50.htm) "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 *,XK0-002 (http://www.testking.net/testking-XK0-002.htm)
this process is particularly important in positional numeral systems for denoting numbers. For a given integer b the positional numeral syste
saheera888
01-11-2010, 03:42 AM
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.
________________________________________
house painter massachsuetts (http://www.housepaintermassachusetts.com)
Scottsdale Foreclosures (http://www.scottsdalelenderforeclosure.com)
davitz38
01-20-2010, 08:51 AM
Hi guys,
I'm sure this tool can help, I use it to make sure I'm converting the right way
Use this decimal to binary converter (http://www.stringfunction.com/decimal-binary.html)
David
hemina
01-25-2010, 07:05 AM
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]);
Powered by vBulletin® Version 4.1.8 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.