-4

I'm wanting to create a basic program to enable me to convert binary numbers into decimal numbers, but having looked almost everywhere on the internet, I just can't find a solution to this that works! I can't seem to follow the solutions, So far I've developed a bit of code, but not sure if it is correct or not, any help? Thanks

           int iBinaryNum; //To store binary number
           string sDecimalNum; //To store decimal numbers

           Console.WriteLine("Enter the binary number you want to convert to decimal");
           iBinaryNum = Convert.ToInt32(Console.ReadLine());

           Console.WriteLine("The Binary number you have entered is " + iBinaryNum);


           sDecimalNum = Convert.ToString(iBinaryNum, 2);

           Console.WriteLine("This converted into decimal is " + sDecimalNum);

           //Prevent program from closing
           Console.WriteLine("Press any key to close");
           Console.ReadKey();
Tom
  • 159
  • 1
  • 3
  • 15
  • 4
    _Everywhere_ on the internet? You didn't look [here](http://stackoverflow.com/questions/9742777/binary-to-decimal-conversion-formula) –  Nov 14 '13 at 01:11
  • I did look there actually, tried the solutions they offered, but didn't seem to work?? – Tom Nov 14 '13 at 01:12

3 Answers3

1

The Convert.ToInt32 method has an overload which accepts a "from" base parameter. http://msdn.microsoft.com/en-us/library/1k20k614.aspx

iDecimalNum = Convert.ToInt32(binaryNumber, 2);

clearly you haven't looked hard enough How to convert binary to decimal

Community
  • 1
  • 1
Duy
  • 1,332
  • 1
  • 10
  • 19
  • I tried adding this but it says "binaryNumber" doesn't exist in context? I apologise, I'm really new to C#, and really need some help with it to get my feet off the ground, sorry to be a pain to everyone – Tom Nov 14 '13 at 01:18
0

First, why do you use an int for the iBinaryNum?

Second, I would have put it in a string, and then rinse and repeat the following:

  • have a counter, starting with 1
  • take last digit, if it's 0, do nothing, if it's 1 multiply it by counter, add to temp result, multiply counter by 2.
  • repeat with second from last ...

so, for 1010 , you'll have 0*1 + 1*2 +0*4 +1*8 = 10.

Here's another page : http://www.binaryhexconverter.com/binary-to-decimal-converter

Edit:
Well, to begin with, what you're asking for is for me to write your code.

All your really need is to figure out how to use loops (look here: http://csharp-station.com/Tutorial/CSharp/Lesson04 )

How to figure out the length of your string : string_name.Length

and then just run on your input from back to front (from length, down to 0), applying the algorithm.

If you really want to learn, follow the bread crumbs trail ...
If you just want someone to write your code ... well ... maybe someone else will ...

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • You've lost me, I'm pretty new with C#, so unsure on how i'd even do a counter, but i'll look into it, thanks for the response. – Tom Nov 14 '13 at 01:19
  • When people give me the solutions though it is beneficial because I take that code and analyse it, and learn from it myself, I'm never that great at working something out from scratch, thanks though, I will take a look at that tutorial now – Tom Nov 14 '13 at 01:56
0

You shouldn't say you've looked every and couldn't find an answer. I understand you're new to this but a better phrase would be: 'I'm having trouble understanding the solutions I've found.'

Anyway, you can use an overload of ToInt32 to convert in a specified base.

iDecimalNum = Convert.ToInt32(iBinaryNum, 2);

Although, to do this, IDecimalNum needs to be a string. The edited code looks like this:

string iBinaryNum = Console.ReadLine();
int iDecimalNum   = Convert.ToInt32(iBinaryNum, 2);

It's also a little weird that you want to convert to a decimal but store iDecimalNum as an integer.

See MSDN's documentation on the overload: http://msdn.microsoft.com/en-us/library/1k20k614(v=vs.110).aspx

Neil Smith
  • 2,565
  • 1
  • 15
  • 18
  • Hello, Yeah your right sorry, I should have listed it how you said, I tried the solution you supplied, but it came up a few errors, so I've changed my variable to a string for the decimal number, and it runs (got no errors) but the only problem is now, It doesn't actually convert the binary number to decimal? this is my code, would be great if you could help me... – Tom Nov 14 '13 at 01:52
  • New code has been updated on the main coding at the top ^^ – Tom Nov 14 '13 at 01:53
  • I see your problem. I'm on my phone and about to be driving so I'll answer back in about an hour. – Neil Smith Nov 14 '13 at 02:15
  • You're converting the user input into an integer and then converting that integer into a string. Instead, you should take in the user input as a string: iBinaryNum = Console.ReadLine(); And then take that string and convert it into an integer with: Convert.ToInt32(iBinaryNum, 2); So you want to take the input as a string and then convert that string to an integer (using 2 to specify you want to convert the string into a base 2 integer. – Neil Smith Nov 14 '13 at 03:34
  • Hey, thanks for that, I've figured it all out now anyways! But thanks for the reply, I actually just realised my code was the otherway around, it was converting decimal numbers into binary other than binary to decimal, so I swapped a few things around and it now works :) – Tom Nov 14 '13 at 23:49