C#二进制转为十进制

using System;using System.Collections.Generic;using System.Text;namespace Demo {class toBinary {static void Main(string[] args) {

编程学习网为您整理以下代码实例,主要实现:C#二进制转为十进制,希望可以帮到各位朋友。

using System;
using System.Collections.Generic;
using System.Text;

namespace Demo {
   class toBinary {
      static voID Main(string[] args) {
         int val = 1010, myBinary, remainder;
         int myDecimal = 0, baseVal = 1;

         myBinary = val;
         while (val > 0) {
            remainder = val % 10;
            myDecimal = myDecimal + remainder * baseVal;
            val = val / 10;
            baseVal = baseVal * 2;
         }

         Console.Write("Binary Number : " + myBinary);
         Console.Write("\nConverted to Decimal: " + myDecimal);
         Console.Readline();
      }
   }
}

本文标题为:C#二进制转为十进制