#include<stdio.h>
int toDecimal(char octal[]) {
int i = 0,num = 0,sign = 1;
if(octal[i] == '-') {
sign = -1;
++i;
}
while(octal[i]) {
if(octal[i] >= '0' && octal[i] <= '7')
num = 8 * num + octal[i] - '0';
++i;
}
return sign * num;
}
char *toOctal(int num,char octal[]) {
int i = 0,t,n = 0,sign = 0;
if(num < 0) {
sign = 1;
num = -num;
}
while(num) {
octal[n] = num % 8 + '0';
num /= 8;
++n;
}
if(sign) octal[n++] = '-';
octal[n] = '\0';
for(i = 0; i < n/2; ++i) {
t = octal[i];
octal[i] = octal[n - 1 - i];
octal[n - 1 - i] = t;
}
return octal;
}
int main() {
int a[10] = {236,-980,+76,1023,8761,3625,+1190,12,-24,64};
char octal[10][12];
int i;
for(i = 0; i < 10; ++i)
printf("%5d <=> (%s)8\n",a[i],toOctal(a[i],octal[i]));
printf("\n\n");
for(i = 0; i < 10; ++i)
printf("(%5s)8 <=> %d\n",octal[i],toDecimal(octal[i]));
return 0;
}
2019-07-17 22:53:11