C# 中十進位制數的八進位制等價物是什麼?


要在 C# 中獲取十進位制數的八進位制等價物 -

首先,對於十進位制值,使用 while 迴圈,並將餘數儲存在為八進位制設定的陣列中。這裡我們在陣列中找到了它們的模 8。

然後,將數字除以 8 -

while (dec != 0) {

   oct[i] = dec % 8;
   dec = dec / 8;
   i++;
}

讓我們看完整的程式碼。這裡,我們的十進位制數為 12 -

示例

using System;

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

         int []oct = new int[50];

         // decimal
         int dec = 12;

         int i = 0;
         while (dec != 0) {
            oct[i] = dec % 8;
            dec = dec / 8;
            i++;
         }

         for (int j = i - 1; j >= 0; j--)
         Console.Write(oct[j]);

         Console.ReadKey();
      }
   }
}

更新於:2020 年 6 月 21 日

138 次瀏覽

助力你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.