This function is supposed to convert decimal to hex, but when I run it instead of letters/appropriate ints in front it outputs 3 as the first int,
eg 17(converted is supposed to be 11)->comes out 31
Could someone pleaaase look at my code and see whats wrong?
(I need it in first principles so I can't use the easy X way)
#include <stdio.h>
void decimal2hex(int num, char hexstring[])
{
int remain=0,x,last=0;
hexstring[last]='\0';
do
{
remain=num%16;
num=num/16;
for(x=last++;x>=0;x--)
hexstring[x+1]=hexstring[x];
hexstring[0]=remain>9?'A'+(remain-10):'0'+remain;
}while(num!=0);
}
int main(int argc, char *argv[])
{
int num;
char hexstring[100];
num=getchar();
decimal2hex(num,hexstring);
printf("%s\n",hexstring);
return 0;
}