the easiest way is to use sprintf with "%X" format specifier
int main()
{
char buf[255] = {0};
int x = 1234;
sprintf(buf,"%X", x);
printf("%s\n", buf);
return 0;
}
Ancient Dragon
Retired & Loving It
30,043 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
You made me laugh with your post to convert decimal to hex, including "A,B...G"
*G* ?? :D
Anyway, your algorithm to change decimal to hexadecimal is wrong. Here's a modded version of your program, which shows what the hex value of a decimal, should be:
#include <stdio.h>
int main(void)
{
int hexa = 16, deci, quotient, ctr;
printf("enter decimal number: ");
scanf("%d",&deci);
for (ctr = 1; ctr<=deci; ctr++)
quotient = deci / hexa;
printf("Equivalent in Hexadecimal is %d",quotient);
printf("\n\n Try this for Hexadecimal: %X", deci);
getche();
return 0;
}
Good luck with your studies.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
Not to be nitpicky, but getche() isn't standard C. You should use getchar() instead.
And here's a link about scanf () (or did I give you it before?)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Did you learn about arrays yet? You could store everything in an array and then print them out in reverse order.
You could also change the calculation. Look into the pow() function
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
There no any reason on why do we have to convert it binary to hex. At at least i havn;t come across any application which need a that feature!
I have.
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943