-3

Okay so, Having tried loads of the solutions found on other questions and on google, I can't seem to find the solution to this problem.. Here is my code, any help appreciated, it is driving me crazy, 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
  • I don't see a description of your problem anywhere. What's wrong with it? – Gabe Nov 14 '13 at 02:09
  • If I am not mistaken, you convert exactly the other way! Try entering `8` and see, if it outputs `100` – Eugen Rieck Nov 14 '13 at 02:10
  • It doesn't convert the binary number into decimal, It just displays a weird number consisting of 0's and 1's, any idea how to fix? sorry for being brief. – Tom Nov 14 '13 at 02:11
  • 1
    You asked this an hour ago !!! here : – Noctis Nov 14 '13 at 02:13
  • @EugenRieck Yeah it outputted 1000, So I see this is the binary for 8, that's a shame, are you sure there is no way at all to convert this way? I've been trying to work it out for ages – Tom Nov 14 '13 at 02:13
  • @Noctis Yeah ik, but I got no help, and I've updated the code since then, it may seem the same, but I edited the post, but had to repost because no one was taking notice of it. – Tom Nov 14 '13 at 02:14
  • You only need to swap things - look at the answer by @Fredou – Eugen Rieck Nov 14 '13 at 02:14
  • @user2985995 the reason they stopped answering is because it didn't seem you're even trying ... – Noctis Nov 14 '13 at 02:15
  • @Noctis ofc I was trying.. I've been trying to solve this problem for weeks, I'm new to C#.. We all started somewhere mate, the site is here in place for people to ask questions, I'm not looking for an argument, I've came here to learn, if your not happy, just keep your comments to yourself, pointing out that I asked it an hour ago, well done captain obvious. – Tom Nov 14 '13 at 02:18
  • There's nothing wrong with learning new things, but when I read a comment like `I tried adding this but it says "binaryNumber" doesn't exist in context?` and then look at the code posted so far, I would probably ask you to review what variables are and talking about how copy-pasting is not an effective way to learn. – MxLDevs Nov 14 '13 at 02:35
  • how is it copying and pasting? I developed the main part of the code myself, the only section which I needed help on was converting it to base 2, which someone assisted me with, and I now know that knowledge.. Seems as if half the people who gave me answers aint got a clue themselves, quite clearly.. else it wouldn't have gave me a load of errors. The main issue was pointed out to me in this post, so therefore I have learnt from it, theres a difference between asking for the entire code or parts of the code.. and then analysing the code for my benefit, I actually want to learn this stuff.. – Tom Nov 14 '13 at 02:43

1 Answers1

0

you have it in the wrong way

    static void Main(string[] args)
    {
        string sBinaryNum; //To store binary number
        int iDecimalNum; //To store decimal numbers

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

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


        iDecimalNum = Convert.ToInt32(sBinaryNum, 2);

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

        //Prevent program from closing
        Console.WriteLine("Press any key to close");
        Console.ReadKey();
    }
Fredou
  • 19,848
  • 10
  • 58
  • 113
  • Thanks a lot! Didn't realise it was so simple, I didn't even realise I had it the other way around until eugen rieck pointed it out above, I'll try that now, don't see why it won't work ofc though, thanks so much! :) – Tom Nov 14 '13 at 02:16
  • Accept his answer if it helped you. – Dayan Nov 14 '13 at 02:17
  • Yeah I will do :) Won't let me at the minute, says 6 minutes remaining – Tom Nov 14 '13 at 02:19