如何将整数转换为二进制表示?

我正在使用下面的代码:

String input = "8";
String output = Convert.ToInt32(input, 2).ToString();

但是它抛出了一个异常:

找不到任何可解析的数字


当前回答

这个函数在c#中将整数转换为二进制:

public static string ToBinary(int N)
{
    int d = N;
    int q = -1;
    int r = -1;

    string binNumber = string.Empty;
    while (q != 1)
    {
        r = d % 2;
        q = d / 2;
        d = q;
        binNumber = r.ToString() + binNumber;
    }
    binNumber = q.ToString() + binNumber;
    return binNumber;
}

其他回答

class Program{

   static void Main(string[] args){

      try{

     int i = (int)Convert.ToInt64(args[0]);
         Console.WriteLine("\n{0} converted to Binary is {1}\n",i,ToBinary(i));

      }catch(Exception e){

         Console.WriteLine("\n{0}\n",e.Message);

      }

   }//end Main


        public static string ToBinary(Int64 Decimal)
        {
            // Declare a few variables we're going to need
            Int64 BinaryHolder;
            char[] BinaryArray;
            string BinaryResult = "";

            while (Decimal > 0)
            {
                BinaryHolder = Decimal % 2;
                BinaryResult += BinaryHolder;
                Decimal = Decimal / 2;
            }

            // The algoritm gives us the binary number in reverse order (mirrored)
            // We store it in an array so that we can reverse it back to normal
            BinaryArray = BinaryResult.ToCharArray();
            Array.Reverse(BinaryArray);
            BinaryResult = new string(BinaryArray);

            return BinaryResult;
        }


}//end class Program

非常简单,没有额外的代码,只是输入,转换和输出。

using System;

namespace _01.Decimal_to_Binary
{
    class DecimalToBinary
    {
        static void Main(string[] args)
        {
            Console.Write("Decimal: ");
            int decimalNumber = int.Parse(Console.ReadLine());

            int remainder;
            string result = string.Empty;
            while (decimalNumber > 0)
            {
                remainder = decimalNumber % 2;
                decimalNumber /= 2;
                result = remainder.ToString() + result;
            }
            Console.WriteLine("Binary:  {0}",result);
        }
    }
}

8位的只有一行

Console.WriteLine(Convert.ToString(n, 2).PadLeft(8, '0'));

n在哪里

    int x=550;
    string s=" ";
    string y=" ";

    while (x>0)
    {

        s += x%2;
        x=x/2;
    }


    Console.WriteLine(Reverse(s));
}

public static string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

这是一个有趣的阅读,我正在寻找一个快速复制粘贴。 我知道我很久以前用bitmath做过这件事。

以下是我的看法。

// i had this as a extension method in a static class (this int inValue);

public static string ToBinaryString(int inValue)
{
    string result = "";
    for (int bitIndexToTest = 0; bitIndexToTest < 32; bitIndexToTest++)
        result += ((inValue & (1 << (bitIndexToTest))) > 0) ? '1' : '0';
    return result;
}

你可以在循环中加入一些模数。

        // little bit of spacing
        if (((bitIndexToTest + 1) % spaceEvery) == 0)
            result += ' ';

你可能会使用或传入一个stringbuilder,并直接追加或索引,以避免释放,也可以绕过+=的使用;