如何使用 C# 将十进制转换为八进制?
为得到八进制等价物,对十进制值使用 while 循环并将余数存储在为八进制设置的数组中。在此,我们将数组中余数模为 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