日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C#?整數(shù)轉(zhuǎn)二進(jìn)制字符串方式_C#教程

作者:JXISH ? 更新時(shí)間: 2023-06-18 編程語(yǔ)言

C# 整數(shù)轉(zhuǎn)二進(jìn)制字符串

C# Int to Binary String

要求

用二進(jìn)制顯示整數(shù),固定長(zhǎng)度,左邊不足補(bǔ)零。

方法

int.tostring沒(méi)有二進(jìn)制格式定義。 詳見(jiàn) Microsoft 標(biāo)準(zhǔn)數(shù)字格式字符串說(shuō)明。

正解,用Convert.ToString:

int c=5;
string d = Convert.ToString(c,2).PadLeft(4,'0');

d: “0101”

C# 輸入任意整數(shù)轉(zhuǎn)成二進(jìn)制

   static void Main(string[] args)
        {
            int[] a = erjinzhi(2021);
            for (int i = 0; i < a.Length; ++i)
            {
                Console.Write(a[i]);
            }
 
        }
        public static int[] erjinzhi(double d)//輸入一個(gè)數(shù)字,將數(shù)字轉(zhuǎn)成二進(jìn)制
        {
            int[] gen = new int[12];//10個(gè)長(zhǎng)度
 
            for (int i = 0; d > 0; ++i)//商=0時(shí)候停止
            {
 
                gen[i] = (int)d % 2;//將余數(shù)轉(zhuǎn)為int類型,存到gen[]數(shù)組中
                d = d / 2;//老商/2作為新被除數(shù)
                d = Math.Floor(d);//向下取整
            }
            Array.Reverse(gen);//將數(shù)組倒敘就得到二進(jìn)制了
            return gen;
        }

總結(jié)

原文鏈接:https://blog.csdn.net/JXISH001/article/details/109447305

  • 上一篇:沒(méi)有了
  • 下一篇:沒(méi)有了
欄目分類
最近更新