如何将二进制转换为十进制

时间:2009-12-25 17:54:45

标签: c# .net

如何将二进制字符串(例如1001101)转换为十进制? (77)

7 个答案:

答案 0 :(得分:62)

Convert.ToInt32方法有overload that accepts a base parameter

Convert.ToInt32("1001101", 2).ToString();

答案 1 :(得分:5)

看看这个非常相似的问题,但处理十六进制How to convert numbers between hexadecimal and decimal in C#?

Convert.ToInt64(value, 2)

答案 2 :(得分:3)

如果您以手动方式而不是使用内置的C#库,这将有效:

static int BinaryToDec(string input)
{
    char[] array = input.ToCharArray();
    // Reverse since 16-8-4-2-1 not 1-2-4-8-16. 
    Array.Reverse(array);
    /*
     * [0] = 1
     * [1] = 2
     * [2] = 4
     * etc
     */
    int sum = 0; 

    for(int i = 0; i < array.Length; i++)
    {
        if (array[i] == '1')
        {
            // Method uses raising 2 to the power of the index. 
            if (i == 0)
            {
                sum += 1;
            }
            else
            {
                sum += (int)Math.Pow(2, i);
            }
        }

    }

    return sum;
}

答案 3 :(得分:1)

string s=Console.ReadLine();

int b=Convert.ToInt32(s,2);

Console.WriteLine("Input value in base 10 = "+b);

将任何二进制文件转换为十进制。 :)

答案 4 :(得分:0)

我想要一个解决方案,无论数字大小,该解决方案始终提供32位。这就是我创造的。

public static string ConvertUintToBitString(uint Number)
{
    string _BitString = string.Empty;

    if (Number >= 2147483648)
    {
        _BitString += '1';
        Number = Number - 2147483648;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 1073741824)
    {
        _BitString += '1';
        Number = Number - 1073741824;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 536870912)
    {
        _BitString += '1';
        Number = Number - 536870912;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 268435456)
    {
        _BitString += '1';
        Number = Number - 268435456;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 134217728)
    {
        _BitString += '1';
        Number = Number - 134217728;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 67108864)
    {
        _BitString += '1';
        Number = Number - 67108864;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 33554432)
    {
        _BitString += '1';
        Number = Number - 33554432;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 16777216)
    {
        _BitString += '1';
        Number = Number - 16777216;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 8388608)
    {
        _BitString += '1';
        Number = Number - 8388608;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 4194304)
    {
        _BitString += '1';
        Number = Number - 4194304;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 2097152)
    {
        _BitString += '1';
        Number = Number - 2097152;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 1048576)
    {
        _BitString += '1';
        Number = Number - 1048576;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 524288)
    {
        _BitString += '1';
        Number = Number - 524288;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 262144)
    {
        _BitString += '1';
        Number = Number - 262144;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 131072)
    {
        _BitString += '1';
        Number = Number - 131072;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 65536)
    {
        _BitString += '1';
        Number = Number - 65536;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 32768)
    {
        _BitString += '1';
        Number = Number - 32768;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 16384)
    {
        _BitString += '1';
        Number = Number - 16384;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 8192)
    {
        _BitString += '1';
        Number = Number - 8192;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 4096)
    {
        _BitString += '1';
        Number = Number - 4096;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 2048)
    {
        _BitString += '1';
        Number = Number - 2048;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 1024)
    {
        _BitString += '1';
        Number = Number - 1024;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 512)
    {
        _BitString += '1';
        Number = Number - 512;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 256)
    {
        _BitString += '1';
        Number = Number - 256;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 128)
    {
        _BitString += '1';
        Number = Number - 128;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 64)
    {
        _BitString += '1';
        Number = Number - 64;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 32)
    {
        _BitString += '1';
        Number = Number - 32;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 16)
    {
        _BitString += '1';
        Number = Number - 16;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 8)
    {
        _BitString += '1';
        Number = Number - 8;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 4)
    {
        _BitString += '1';
        Number = Number - 4;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 2)
    {
        _BitString += '1';
        Number = Number - 2;
    }
    else
    {
        _BitString += '0';
    }
    if (Number == 1)
    {
        _BitString += '1';
    }
    else
    {
        _BitString += '0';
    }

    return _BitString;
}

答案 5 :(得分:0)


阅读您的问题后,我已经尝试过了。它更长一些,但提供了解决方案。我将二进制元素保存在数组中以获得解决方案。就像我说的那样,可以找到更长,更短的方法。

        // Binary ------> Decimal 
        int len;
        double deci = 0;

        Console.Write("Length of binary number: ");
        len = Convert.ToInt32(Console.ReadLine());

        int[] bnry = new int[len];

        for (int i = 0; i < len; i++)
        {
            Console.Write("{0} index of binary number: ", i);
            bnry[i] = Convert.ToInt32(Console.ReadLine());
        }
        Console.Write("Your binary number: ");
        for (int i = len - 1; 0 <= i; i--)
        {

            Console.Write(bnry[i]);

        }

        Console.Write("\nDecimal number: ");
        for (int i = 0; i < len; i++)
        {
            deci += (bnry[i] * Math.Pow(2, i));
        }
        Console.Write(deci);

答案 6 :(得分:0)

使用 binary stringuintbinary or 转换为 shift 的手动方法可能是:

  public static uint ConvertBinaryStringToUInt32(string binaryString)
  {
     if (binaryString is null)
     {
        throw new ArgumentNullException(nameof(binaryString));
     }

     if (binaryString.Length > 32)
     {
        throw new ArgumentOutOfRangeException(nameof(binaryString), binaryString.Length, "The specified binary string can not be longer than 32 characters.");
     }

     uint result = 0u;

     for (int i = 0; i < binaryString.Length; i++)
     {
        result <<= 1;

        char c = binaryString[i];

        if (c == '0')
        {
        }
        else if (c == '1')
        {
           result |= 1u;
        }
        else
        {
           throw new FormatException($"Character {i} of binary string \"{binaryString}\" is an invalid '{c}'. Can only be '0' or '1'.");
        }
     }

     return result;
  }