十进制八进制二进制之间的转换代码C语言
时间: 2023-07-09 20:05:06 浏览: 100
以下是C语言实现十进制、八进制和二进制之间的转换代码:
从十进制转换为八进制:
```
void Decimal2Octal(int decimalNumber) {
int quotient = 0;
int octalNumber[100];
int i = 0;
quotient = decimalNumber;
while (quotient != 0) {
octalNumber[i] = quotient % 8;
quotient = quotient / 8;
i++;
}
printf("Octal number is: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", octalNumber[j]);
}
}
```
从十进制转换为二进制:
```
void Decimal2Binary(int decimalNumber) {
int quotient = 0;
int binaryNumber[100];
int i = 0;
quotient = decimalNumber;
while (quotient != 0) {
binaryNumber[i] = quotient % 2;
quotient = quotient / 2;
i++;
}
printf("Binary number is: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binaryNumber[j]);
}
}
```
从八进制转换为十进制:
```
int Octal2Decimal(int octalNumber) {
int decimalNumber = 0;
int i = 0;
int remainder;
while (octalNumber != 0) {
remainder = octalNumber % 10;
octalNumber = octalNumber / 10;
decimalNumber = decimalNumber + remainder * pow(8, i);
i++;
}
return decimalNumber;
}
```
从八进制转换为二进制:
```
void Octal2Binary(int octalNumber) {
int decimalNumber = Octal2Decimal(octalNumber);
Decimal2Binary(decimalNumber);
}
```
从二进制转换为十进制:
```
int Binary2Decimal(long long binaryNumber) {
int decimalNumber = 0;
int i = 0;
int remainder;
while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber = binaryNumber / 10;
decimalNumber = decimalNumber + remainder * pow(2, i);
i++;
}
return decimalNumber;
}
```
从二进制转换为八进制:
```
void Binary2Octal(long long binaryNumber) {
int decimalNumber = Binary2Decimal(binaryNumber);
Decimal2Octal(decimalNumber);
}
```
阅读全文