เพื่อให้ได้ค่าเทียบเท่าฐานแปดของทศนิยมใน C# −
ประการแรก สำหรับค่าทศนิยม ใช้ while loop และเก็บส่วนที่เหลือไว้ในอาร์เรย์ที่ตั้งค่าไว้สำหรับฐานแปด ที่นี่เราพบ 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();
}
}
}