Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C# Binary -> Decimal Converter Outputting large numbers outside of reason...

DolF1NN

New Coder
When I input a binary number into the string variable "binary" say "0000" it returns 720, the number seems to be based on how many characters are in the string Binary, and the number exponentially increases the more digits. I dont know why this is... Could someone help me out?

My code is:

Code:
using System;
                    
public class Program
{
    public static void Main()
    {
        string binary = "0000";
        int binaryLength = binary.Length;
        double binaryInt = 0;
        for(int i = binaryLength; i > 0; i--)
        {
            binaryInt += (binary[i - 1]) * (Math.Pow(2, binaryLength - i));
            Console.Write(binary[i - 1] + "  ");
            Console.WriteLine(Math.Pow(2, binaryLength - i) + "   ");
            
        }
        Console.WriteLine();
        Console.WriteLine(binaryInt);
    }
}
 
When I input a binary number into the string variable "binary" say "0000" it returns 720, the number seems to be based on how many characters are in the string Binary, and the number exponentially increases the more digits. I dont know why this is... Could someone help me out?

My code is:

Code:
using System;
               
public class Program
{
    public static void Main()
    {
        string binary = "0000";
        int binaryLength = binary.Length;
        double binaryInt = 0;
        for(int i = binaryLength; i > 0; i--)
        {
            binaryInt += (binary[i - 1]) * (Math.Pow(2, binaryLength - i));
            Console.Write(binary[i - 1] + "  ");
            Console.WriteLine(Math.Pow(2, binaryLength - i) + "   ");
       
        }
        Console.WriteLine();
        Console.WriteLine(binaryInt);
    }
}
You're seeing unexpected results because you're treating the characters in the binary string as their ASCII values, not actual binary digits.

Add:

C#:
// Get the binary digit character and convert it to its integer value (0 or 1)
int bit = binary[i - 1] - '0';

C#:
using System;

public class Program
{
    public static void Main()
    {
        string binary = "0000";
        int binaryLength = binary.Length;
        double binaryInt = 0;
        for(int i = binaryLength; i > 0; i--)
        {
            int bit = binary[i - 1] - '0';
            binaryInt += bit * Math.Pow(2, binaryLength - i);
            Console.Write(bit + "  ");
            Console.WriteLine(Math.Pow(2, binaryLength - i) + "   ");
        }
        Console.WriteLine();
        Console.WriteLine(binaryInt);
    }
}
 
Last edited:
You're seeing unexpected results because you're treating the characters in the binary string as their ASCII values, not actual binary digits.

Add:

C#:
// Get the binary digit character and convert it to its integer value (0 or 1)
int bit = binary[i - 1] - '0';

C#:
using System;

public class Program
{
    public static void Main()
    {
        string binary = "0000";
        int binaryLength = binary.Length;
        double binaryInt = 0;
        for(int i = binaryLength; i > 0; i--)
        {
            int bit = binary[i - 1] - '0';
            binaryInt += bit * Math.Pow(2, binaryLength - i);
            Console.Write(bit + "  ");
            Console.WriteLine(Math.Pow(2, binaryLength - i) + "   ");
        }
        Console.WriteLine();
        Console.WriteLine(binaryInt);
    }
}
or:

C#:
using System;

public class Program
{
    public static void Main()
    {
        string binary = "0000";
        int decimalValue = Convert.ToInt32(binary, 2);
        Console.WriteLine($"Binary: {binary}");
        Console.WriteLine($"Decimal: {decimalValue}");
    }
}
 
Back
Top Bottom