我只想知道转化的原理,不要用程序自带函数去转 也不用计算器,只用一只笔去得到结果。比如 
100  ->1100100
101  ->1100101还有 负数能转成二进制数么 怎么转? 比如-100
还有 小数能赚成二进制么 怎么转?  比如 120.54

解决方案 »

  1.   

    遇2进1如3-> 11
      4->100
    ...
      

  2.   

    问题一:正负是由标志位决定的,比如一个int32类型,里面会有一个bit表明是正书还是负数。
    问题二:小数和整数一样的方法,只不过小数是2的-n次方累加,整数是2的n次方累加
      

  3.   

     static void Main(string[] args)
            {
                double s = 12.34;
                string str = "";
                jzzh((int)s, ref str);
                str += ".";
                xsjzzh(s - (int)s, ref str, 0);
                Console.ReadLine();
            }
    //转换整数
            static void jzzh(int n, ref string s)
            {
                if (n / 2 != 0)
                    jzzh(n / 2, ref s);
                s += n % 2;
            }
    //转换小数
            static void xsjzzh(double n, ref string s, int sum)
            {
                if (sum > 20 || n * 2 <= 0.000001)
                {
                    return;
                }
                s += (int)(n * 2);
                xsjzzh(n * 2 - (int)(n * 2), ref s, sum + 1);
            }
      

  4.   

    double d = 123.456;
    string bin = "";
    int d1 = (int)d;
    while (d1 > 0)
    {
        bin = (d1 % 2 == 1 ? "1" : "0") + bin;
        d1 = d1 / 2;
    }
    double d2 = d - (int)d;
    bin = bin + ".";
    double x = 0.5;
    for (int i = 0; i < 10; i++)
    {
        bin = bin + (d2 >= x ? "1" : "0");
        if (d2 >= x)
            d2 = d2 - x;
        x = x / 2.0;
    }
    Console.WriteLine(bin);