user3832863
user3832863

Reputation: 97

Binary to Decimal Conversion doesn't work

This is kind of a funky program. For some reason it works when the binary input is something like 101. Then it doesn't for 1000. This is kind of odd. Could someone please explain?

class Program
{
    static void Main()
    {         
            string binary = "xxx";
            double decimalValue = 0;

            Console.WriteLine("Enter in a binary number:");
            binary = Console.ReadLine();

            for (int i = 0; i < binary.Length; i++)
            {
                Console.WriteLine("Length is: {0}", binary.Length);

                if (binary[i] == 49)  //Look at that
                    decimalValue = decimalValue + Math.Pow(2, i);
            }

            Console.WriteLine("The decimal equivalent value is {0}", decimalValue);

            Console.ReadLine();
    }
}

The heart of it is of course if (binary[i] == 49)

I'm just making it to teach myself some C#. Could someone tell me what to put on the right side other than 49, which is the ASCII number for "1". If I put "1" I get an error saying you can't compare a string to a char.

Any help would be appreciated. I don't want to use the pre-canned convert to binary method, because this is supposed to be a teachable moment.

Upvotes: 0

Views: 100

Answers (2)

venerik
venerik

Reputation: 5904

Instead of trying to add the value of each individual bit based on it's position you could take another approach: shift and add. In this approach you shift the current value to the left (by multiplying that value by 2) and adding the current bit.

For instance: the binary value 1010 becomes decimal 10 in four cycles:

value = 0
value *= 2 => value = 0
value += bit 1 => value = 1
value *= 2 => value = 2
value += bit 0 => value = 2
value *= 2 => value = 4
value += bit 1 => value = 5
value *= 2 => value = 10
value += bit 0 => value = 10

Or, in code:

using System;

public class Program
{
    public static void Main()
    {
        string binary = "";
        double decimalValue = 0;

        Console.WriteLine("Enter in a binary number:");
        binary = Console.ReadLine();

        for (int i = 0; i < binary.Length; i++)
        {
            decimalValue *=2; // shift current value to the left
            if (binary[i] == 49)
            {
                decimalValue += 1; // add current bit
            }   
            Console.WriteLine("Current value: {0}", decimalValue);
        }
        Console.WriteLine("The decimal equivalent value is {0}", decimalValue);
        Console.ReadLine();
    }
}

Upvotes: 1

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61912

You read the characters from the wrong end.

As was said immediately in the first comment to the question, by Lucas Trzesniewski, replace one use of i (not both) inside the for loop with binary.Length - 1 - i.

The reason why it works for "101" is that this is a palindrome (reads the same backwards).

Note: 49 is the ASCII code for '1'. It is more readable to use == '1' than == 49. However, both work equally well. In C# you get a char value if you use single quotes, as in '1', and you get a string object reference if you use double quotes, "1".

You should remove the stuff with "xxx". It has no function. Just dostring binary = Console.ReadLine();.

Upvotes: 1

Related Questions