Question (Integers)

Discussion in 'C' started by gatsbycollege, Dec 28, 2011.

  1. gatsbycollege

    gatsbycollege New Member

    Joined:
    Dec 27, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Philippines
    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 a moderator: Dec 28, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice