PDA

View Full Version : Binary Converter



Korpse
04-20-2005, 07:54 AM
hi

wandering if any of you kind chaps could program a bianry converter for help with my CISCO CCNA course

would be kewl if i could type in a decimal number and click a button and it would convert it into binary and vice versa

any1 willing to take this up?

thanks very much

Korpse

winterburnd
04-20-2005, 02:17 PM
Well in my programming course last semester we had to do conversions like that. The best tool I found was using the windows calculator. Only downside is that if there is a zero at the start it won't show until there is finally a one. :(
So it is basically right when you think about it but its not.

Best I know for you.

Disposibleteen
04-20-2005, 07:22 PM
there are a bunch of binary converters online(find with google), not sure how accurate they are though.

eshbach
04-20-2005, 09:16 PM
if you still want this i can write you one quickly...

Korpse
04-20-2005, 10:40 PM
if you could m8 - as i dont have 24/7 internet acces (56k baby)

and would be useful for when i have coursework etc to do at home

cheers

wr0x2
04-21-2005, 02:51 AM
My friend did this on his TI83+ in ti basic, so it can't be a very hard task. What language is this supposed to be done in?

Korpse
04-21-2005, 02:52 AM
english...


or do u mean coding language? if so i dont have a clue

winterburnd
04-21-2005, 09:51 AM
The TI83+ is a graphing calc.

That program would have been nice to have last semester in programing... :p:

eshbach
04-21-2005, 12:18 PM
ok, done with this.

supports up to 32 bit numbers

if you need any other features let me know.

i wanted to get this done quickly so it's in C#. You'll need the .NET framework to run it i think.

Korpse
04-21-2005, 11:15 PM
cheers m8

will test it later 2nite (college comps done allow us to run .exes :()

matt9669
04-23-2005, 03:22 PM
Works great for me, though I do have .NET installed (not exactly difficult, just grab it off Windows Update) :toast:

winterburnd
04-23-2005, 03:29 PM
Nice program. I raise my glass as well. :toast:

eshbach
04-23-2005, 03:38 PM
good to hear it's working. for anyone who's wondering, here's the C# code for the actual conversion:



private void button1_Click(object sender, System.EventArgs e)
{
try
{
//get input
double input = Convert.ToDouble(textBox1.Text);
//convert decimal to binary
double temp = 0.0;
int[] bits = new int[32];
for (int i = 31; i >= 0; --i)
{
if (Math.Pow(2,i) + temp <= input)
{
bits[i] = 1;
temp += Math.Pow(2,i);
}
else
{
bits[i] = 0;
}
}
for (int i = 31; i >= 0 ; --i)
{
if ((i+1)%8==0)
{
result += " ";
}
result += ""+bits[i];
}
label1.Text = result;

}
catch(System.Exception)
{
label1.Text = "Please Enter a Valid Number";
}

}

matt9669
04-23-2005, 03:49 PM
Is "try" a C# reserved word? Never seen it before - of course, that could just be because :am: ;)

eshbach
04-23-2005, 04:51 PM
Is "try" a C# reserved word? Never seen it before - of course, that could just be because :am: ;)

i use "try" in C++, C#, JAVA, and J#. As far as i know it is a very common feature.

it is used in conjunction with catch and/or finally in handling exceptions. if an exception is thrown in the try block, it can be caught in a catch block at which point you can tell the system what to do in the event of a specific exception, or as in my code above, in the case of any exception. finally will excecute after the try block regardless of whether or not any exceptions were thrown/caught.

In my program, the most common exception would be to put in a "bad" number, i.e. something with a letter or a non-numeric character. if that happens, the Conver.ToDouble() function would throw an exception which i catch and then prompt for a valid number.

the example below shows the general usage for try/catch/finally blocks:


try
{
//do something
}
catch ("some exception")
{
//do something
}
catch("some other exception")
{
//do something
}
catch("generic exception")
{
//do something
}
finally
{
//do something
}

matt9669
04-23-2005, 05:02 PM
That would explain it - we've never been required to handle exceptions in our programs, it is assumed that the input is valid.

total_assault
04-24-2005, 06:48 AM
nice work, your using this for subnetting in your cisco course rite?

Korpse
04-25-2005, 12:07 AM
yes m8

and im cack at turning binary into numbers and vice versa :D hehe

this should help in the semester 1 main test on wednesday :p

jmg823
04-28-2005, 03:34 PM
My friend did this on his TI83+ in ti basic, so it can't be a very hard task. What language is this supposed to be done in?

I wrote a binary-hex:hex-binary converter in BASIC. I still have it if you want it, it currently is kind of messy but it works. It can not do infinate it can convert numbers up to about 8.5 million. Im only a freshman in high school so i really have no need for this program, but if someone wants it ill finish it and make it better.