PDA

View Full Version : problem:decimal to hex


Erev0s
05-03-04, 06:44 PM
Hi i want to convert a decimal number to a hex with this recursive function
I have a problem:
ie: (205)10=(CD)16
i have a problem with my code it'doesnt print the correct answer :)
plz help



void DecToHex (long dec_numb)

{
new_numb=dec_numb;
dec_numb%=16;
new_numb/=16;
if(new_numb>0){
DecToInt(new_numb);
}
printf("%lX",dec_numb);

}

gopinath_alr
05-04-04, 12:26 AM
hi,
u need not to go for recursive methods.u can use itoa function with the radix value 16. try the following code.

#include <stdio.h>
#include <stdlib.h>

void main()
{
int decNumber=11;
char hexNumber[10];
itoa(decNumber,hexNumber,16);
printf("Hexa decimal number:Ox%s\n",hexNumber);
}