Alright guys, I've been trying to think how to do this and I keep confusing myself.
It's part of a project using piping in UNIX but first I need to get this small program running properly. I want to pass the final result (Hex value) to another program that will convert that result to binary.
This is my current code, which runs fine, although I was thinking of storing the value where is says printf("A"); ,for example, into an array.
Although, I need to pass the array to another program which is looking for an int value. I think the best way is to concatenate this array into a single variable (If possible!) and pass this instead.
int n,r[10],i=0,number,j=0;
printf("Enter a decimal number: ");
scanf("%d",&number);
while(number>0)
{
r[i] = number % 16;
number=number / 16;
i++;
j++;
}
printf("The hexadecimal equivalent is: ");
for(i=j-1;i>=0;i--)
{
if(r[i]==10)
printf("A");
else if(r[i]==11)
printf("B");
else if(r[i]==12)
printf("C");
else if(r[i]==13)
printf("D");
else if(r[i]==14)
printf("E");
else if(r[i]==15)
printf("F");
else
printf("%d",r[i]);
}
printf("\n");
}
What do you guys think?
NOTE: I'm converting this way as we're not supposed to use any short cuts during the conversion, such as methods in libraries or system calls etc.
Thanks,
Dec28