Hi, i'm a newbie to C and currently trying to learn the language, so at the moment i only really know the basic stuff. I'm trying to create a program that converts a decimal number (entered by the user) into hexidecimal. I have got it working for one single, decimal digit, but i have hit a mental block and can't seem to see how i am able to do it for when the decimal number is above 15. Here is what i have so far:

#include <stdio.h>

char hex;
unsigned int decimal; //stores decimal number that needs to be converted.


int main()
{

printf("Please enter the digit: ");
scanf("%u", &decimal);
if (decimal < 10)
hex = '0' + decimal;
else hex = 'A' + (decimal - 10);
printf("%c ",hex);
return 0;
}

If you enter 16 for example, it displays G (as i expected) but obviously it needs to loop round some how so that it displays 10. I'm thinking it would be a FOR loop but again, i can't see how it would work.

Any help would be greatly appreciated! (Please try and keep it as simple as possible, some of the posts on here make my mind blow up. )

>If you enter 16 for example, it displays G (as i expected)
16 is not G in hexadecimal notation. It's 10.

All that you need to display hexadecimal numbers is to use the %x format in printf().

#include <stdio.h>

main()
{
   int value;

   printf("enter any decimal value: ");
   scanf("%d",&value); 
   printf("value: decimal %d = hex %x\n", value, value);

   return 0;
}

thats all you have to do, right?

"int" doesnt have an inherent base. it's whatever you want it to be, whenever you want it to be.

or ....

are you specifically trying to convert it by hand as an exercise?

Thank you for your replies. I wasn't aware of the %x feature, thank you very much that has made it a lot simplier! However, just out of my curiosity, my friend mentioned that i could try the following:

(decimal % 16) = 1s column
(decimal / 16) = 16s column
((decimal / 16) / 16) = 256s column (and so on etc)

This makes sense to me, but unfortuntely he was in a hurry and didnt have time to explain properly. How would i work this into the code that i already have? (In my original post). It would have to go into some kind of loop, but i cant work out which one and how it would work.

While the %x is a good solution, for the purposes of my learning if i can get it working using my orginal code that would be a great help!!! :)

(decimal % 16) = 1s column
(decimal / 16) = 16s column
((decimal / 16) / 16) = 256s column (and so on etc)

You should use the mod operator, and then apply your code, at each iteration: your loop should repeatedly replace 'decimal' by 'decimal/16'.

Ok thanks. What kind of loop should it be? I was thinking a FOR loop, but again i cant seem how it would fit? Is it possible for an example? That would be of great help!

Entirely your choice, but I would use a while loop. I would also use recursion (i.e. your function should call itself within the loop), as you'll need to print out the columns in reverse order to how they're calculated.

Entirely your choice, but I would use a while loop. I would also use recursion (i.e. your function should call itself within the loop), as you'll need to print out the columns in reverse order to how they're calculated.

I wouldn't. I'd just write the loop properly.

This article has been dead for over six months. Start a new discussion instead.