如何使用 C# 将十进制转换成八进制?
若要得到八进制的等价形式,为十进制值使用一个 while 循环,并将余数存储在八进制指定的数组中。这里,我们通过 mod 8 将余数设定在数组中。
然后将该数字除以 8 −
while (dec != 0) {
oct[i] = dec % 8;
dec = dec / 8;
i++;
}让我们来看看完整的代码。
这里,我们的十进制数字为 18 −
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int []oct = new int[30];
// decimal
int dec = 18;
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();
}
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP