C# 程式將十進位制轉換為八進位制數字
設定十進位制數字 −
int decVal = 40;
現在採用一個變數,並在其中設定 **decVal**。由於八進位制採用基數 8 的數字系統,因此用 8 求餘並根據以下程式碼段在迴圈中計算它。
while (quot != 0) { octalVal[i++] = quot % 8; quot = quot / 8; }
示例
可以嘗試執行以下程式碼將十進位制轉換為八進位制數字。
using System; class Demo { public static void Main() { int decVal, quot, i = 1, j; int[] octalVal = new int[80]; decVal = 40; quot = decVal; Console.WriteLine("Decimal Number:{0}",decVal); while (quot!= 0) { octalVal[i++] = quot % 8; quot = quot / 8; } Console.Write("Octal Number: "); for (j = i - 1; j > 0; j--) Console.Write(octalVal[j]); Console.Read(); } }
輸出
Decimal Number:40 Octal Number: 50
廣告