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

學無先后,達者為師

網站首頁 編程語言 正文

C#?整數轉二進制字符串方式_C#教程

作者:JXISH ? 更新時間: 2023-06-18 編程語言

C# 整數轉二進制字符串

C# Int to Binary String

要求

用二進制顯示整數,固定長度,左邊不足補零。

方法

int.tostring沒有二進制格式定義。 詳見 Microsoft 標準數字格式字符串說明。

正解,用Convert.ToString:

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

d: “0101”

C# 輸入任意整數轉成二進制

   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)//輸入一個數字,將數字轉成二進制
        {
            int[] gen = new int[12];//10個長度
 
            for (int i = 0; d > 0; ++i)//商=0時候停止
            {
 
                gen[i] = (int)d % 2;//將余數轉為int類型,存到gen[]數組中
                d = d / 2;//老商/2作為新被除數
                d = Math.Floor(d);//向下取整
            }
            Array.Reverse(gen);//將數組倒敘就得到二進制了
            return gen;
        }

總結

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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新