当前位置:首页 > C#教程 > C#进阶

C#将十进制转换为二进制

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

namespace Demo {
   class MyApplication {
      static void Main(string[] args) {

         int decVal;
         int val;
         string a = "";

         decVal = 34;
         Console.WriteLine("Decimal: {0}", decVal);

         while (decVal >= 1) {
            val = decVal / 2;
            a += (decVal % 2).ToString();
            decVal = val;
         }
         string binValue = "";

         for (int i = a.Length - 1; i >= 0; i--) {
            binValue = binValue + a[i];
         }

         Console.WriteLine("Binary: {0}", binValue);
         Console.Read();
      }
   }
}

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!