C#十进制转二进制

转换类:

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

namespace tento2
{
    class ConvertUtil
    {
        public static string get2Value(int tenValue)
        {
            String ret = "";
            while (tenValue / 2 != 0)
            {
                ret = tenValue % 2 + ret;
                tenValue /= 2;
            }
            if (tenValue != 0)
                ret = tenValue + ret;
            return ret;
        }

        public static string get2ValueRec(int tenValue)
        {
            if (tenValue / 2 == 0 && tenValue!=0)
            {
                return "" + tenValue;
            }

            return get2ValueRec(tenValue / 2) + tenValue % 2;
        }
    }
}

主程序类:

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

namespace tento2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ConvertUtil.get2ValueRec(10));
            Console.ReadLine();
        }
    }
}

程序输出:
1010

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • include <stdlib.h>
    和谐共处阅读 4,332评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,092评论 19 139
  • (一)、进制之间的转换 八进制:0-7 十六进制:0-F 1、十进制 与 二进制之间的转换 (1)、十进制转换为二...
    MPPC阅读 22,158评论 2 49
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,226评论 18 399
  • 轻轻的我走了 可我只是秋 我走了 但我的背影还在 我留下世间最后的静美 留下死亡之前的宣告 轻轻的我走了 可我还会...
    追忆者阅读 1,684评论 0 6

友情链接更多精彩内容