Decimal to Hexadecimal Conversion in C..

Discussion in 'C++' started by owen29, Mar 17, 2009.

  1. owen29

    owen29 New Member

    Joined:
    Mar 10, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello Friends,

    I have to convert Decimal to Hexadecimal,
    I Used this code for it.
    But the problem that i am facing is if i enter Decimal=42
    It shows Hexadecimal =A.

    But it should show Hexadecimal=4A.

    What should be done then...??'

    CODE :

    Code:
    int n,r[10],i;
    clrscr();
    printf("Enter the decimal number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    r[i]=n1%16;
    n=n/16;
    }
    i--;
    for(i=n;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");
    
    
    Thank you..
     
    Last edited by a moderator: Mar 17, 2009
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    r=n1%16;

    what is n1 ?
    instead u can use it..
    Code:
    int main() 
    {
    char hx[256];
    int decVal = 42;
    sprintf(hx,"%x",decVal);
    printf("Hex value of Decimal value %d  is %s\n",decVal,hx);
    }
     
    Last edited: Mar 17, 2009
  3. rahulmaximus

    rahulmaximus New Member

    Joined:
    May 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    njoy!!




    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int n,r[10],i=0,number,j=0;
    printf("Enter the decimal number\n");
    scanf("%d",&number);
    
    
    
    while(number>0)
    {
    r[i]=number%16;
    number=number/16;
    i++;
    j++;
    }
    cout<<"The hexa decimal equivalent is  ";
    for(i=j-1;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");
    
    system("pause");
    }
     
    Last edited by a moderator: Jun 4, 2011
  4. bmsangati

    bmsangati New Member

    Joined:
    Jun 13, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    1. //Decimal to Hex value
    2. main()
    3. {
    4. char hex[16]={"0123456789ABCDEF"};
    5. int num,i=0,j;
    6. char result[];
    7. printf("Enter the Decimal No.\n");
    8. scanf("%d",&num);
    9. while(num)
    10. {
    11. result=hex[num%16];
      [*] num=num=num/16;
      [*] i++;
      [*] }
      [*] printf("Given num of the hex value is:");
      [*] for(j=i-1;j>=0;j--)
      [*] printf("%c",result[j]);
      [*] getch();
      [*] return 0;
      [*] }
     
  5. bmsangati

    bmsangati New Member

    Joined:
    Jun 13, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi friends, this is Decimal to Hex conversion
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    And you have shared the same in every possible section of the forum. I know you are excited about it but that does not mean you can post 4 times.
     
  7. anishsina

    anishsina New Member

    Joined:
    Jul 5, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Re: Decimal to Hexadecimal Conversion in C

    #include<stdio.h>
    #include<conio.h>
    main
    {
    clrscr();
    int a=42;
    printf("%x",a); // to obtain octa decimal number use "%o" instead of "%x"
    getch();
    }

    o/p: 2a
     
  8. debugEnthu

    debugEnthu New Member

    Joined:
    Jul 5, 2012
    Messages:
    11
    Likes Received:
    3
    Trophy Points:
    0
    Slight modification to yr can give u the expected result
    Code:
    #include <stdio.h>
    
    int main ()
    {
    
    int n,r[10],i = -1, j = 0 ;
    printf("Enter the decimal number\n");
    scanf("%d",&n);
    while(n)
    {
    i++;
    r[i]=n%16;
    n=n/16;
    }
    
    for(j=i;j>=0;j--)
    {
    if(r[j]==10)
    printf("A");
    else if(r[j]==11)
    printf("B");
    else if(r[j]==12)
    printf("C");
    else if(r[j]==13)
    printf("D");
    else if(r[j]==14)
    printf("E");
    else if(r[j]==15)
    printf("F");
    else
    printf("%d",r[j]);
    }
    printf("\n");
    
    }
    ~
     
    Last edited by a moderator: Jul 6, 2012
  9. C2E

    C2E New Member

    Joined:
    Dec 29, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi! I was also looking for the same. I tried it out myself.
    Please take a look at this & try it out.
    Is this correct as per your requirements? I feel you may wana debug it for the logic. but the code is self explanatory.
    Before the function ends, someone would wana add a reverse string revstr that counts the array. Im not good with pointers as such but the intention of pointer should be clear. Similarly, during rev str the pointer would keep decrementing & print the values from char arrays.
    Do reply & let me know if it worked out!
    Thanks.
    Code:
     
    char hex_ret(int X)
    {
     int quotient;
     int rem;
     char i[16]; void *p=i;
     char r[]={'A','B','C','D','E','F','/0'};
     
       if (X>=16)
       {
        quotient=X/16;
        rem=X%16;
        if (quotient>=16)
        {
         if (rem>9)
         {
          int finalchar=rem-9;
          *p=r[finalchar-1];
          p++;
          hex_ret(quotient);
         }  
         else
         {
          *p=rem;
          p++;
          hex_ret(quotient);
         }  
        } 
        else
        {
         if (quotient>9 && quotient<=15)
         {
          *p=rem;
          p++;
          int finalchar=quotient-9;
          *p=r[finalchar-1];
          *p++='/0';
         }
         else
         {
          *p=rem;
          p++;
          *p=qoutient;
          *p++='/0';
         }
     
        }
       } 
       else
       {
        int finalchar=quotient-9;
        *p=r[finalchar-1];
       }  
    }
     

    Attached Files:

    Last edited by a moderator: Dec 30, 2012

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