Please Help Place your answer to this question in a file named \"convert .c\". W
ID: 3637655 • Letter: P
Question
Please Help
Place your answer to this question in a file named "convert .c". Write a C program called convert that takes three command line arguments 1: a numeral, an input base (expressed in decimal), and an output base (also in decimal), It should print the value represented by the numeral as interpreted in the input base, translated into the output base. For example: The program should work with any input and output radix between 2 and 36, using the digits 0 to 9 and a to z. Hint: Convert to internal integer form as an intermediate .step. You may not call the C library functions atoi ( ) or strtol ( ) or any other library functions to convert, the input numeral.Explanation / Answer
#include <stdio.h>
int value(char c){
if(c>='0' && c<='9') return c-'0';
else return (c|32)-97 + 10;
}
char tochar(int n) {
if(n<=9)
return '0'+n;
return 'A'+n-10;
}
int conv(char*c){ int s = 0; while(*c) { s=s*10+(*c - '0'); c++; } return s;}
int main(int argc, char** argv){
if(argc <4)
{
printf("Usage: convert input_num in_base out_base ");
return 0;
}
char *val = argv[1];
int b_in = conv(argv[2]);
int b_out = conv(argv[3]);
int res = 0;
int ind = 0;
while(*val){
res = res*b_in+value(*val);
val++;
}
if(!res) {
printf("0 ");
return 0;
}
char resout[100];
char *p = &resout[99];
*p='';
while(res>0){
p--;
*p=tochar(res%b_out);
res/=b_out;
}
printf("%s ", p);
return 0;
}