Question (Integers)

gatsbycollege's Avatar, Join Date: Dec 2011
Light Poster
Code:
void Dec2Hex()
{
    int n,r[10],i;
    printf("Enter a number to get its hexadecimal equivalent\n");
    scanf("%d",&n);
    for(i=0;n!=0;i++)
    {
        r[i]=n%16;
        n=n/16;
    }
    i--;
    for(;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");
}
is it possible to increase the maximum input of conversion here?,, i cant convert decimal to hex that is exceeding 32k

Last edited by shabbir; 28Dec2011 at 15:54.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Yes, but you can't use scanf %d. I suggest you use fgets to read whatever the user gives you, then convert whatever they give you to hex. It's not difficult.