十進位制數在 C# 中的八進位制等效是什麼?


獲取十進位制數在 C# 中的八進位制等效值 −

首先,對於十進位制值,使用 while 迴圈,並將餘數儲存在八進位制集的陣列中。此處,我們在陣列中找到了它們 mod 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();
      }
   }
}

更新於: 21-06-2020

138 次瀏覽

開始您的職業生涯

完成課程即可獲得認證

操作指南
廣告
© . All rights reserved.