Decimal to Hexadecimal Conversion in C..

Newbie Member
17Mar2009,18:23   #1
owen29's Avatar
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 shabbir; 17Mar2009 at 18:58.. Reason: Code blocks
TechCake
17Mar2009,20:49   #2
asadullah.ansari's Avatar
r[i]=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 by asadullah.ansari; 17Mar2009 at 20:54..
Go4Expert Member
4Jun2011,03:30   #3
rahulmaximus's Avatar
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 shabbir; 4Jun2011 at 09:47.. Reason: Code blocks
Newbie Member
13Jun2012,13:28   #4
bmsangati's Avatar
  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[i]=hex[num%16];
  12. num=num=num/16;
  13. i++;
  14. }
  15. printf("Given num of the hex value is:");
  16. for(j=i-1;j>=0;j--)
  17. printf("%c",result[j]);
  18. getch();
  19. return 0;
  20. }
Newbie Member
13Jun2012,13:29   #5
bmsangati's Avatar
Hi friends, this is Decimal to Hex conversion
Go4Expert Founder
13Jun2012,13:31   #6
shabbir's Avatar
Quote:
Originally Posted by bmsangati View Post
Hi friends, this is Decimal to Hex conversion
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.
Newbie Member
5Jul2012,21:11   #7
anishsina's Avatar
#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
Go4Expert Member
6Jul2012,12:43   #8
debugEnthu's Avatar
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 shabbir; 6Jul2012 at 14:31.. Reason: Code blocks
C2E
Newbie Member
30Dec2012,03:56   #9
C2E's Avatar
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
File Type: txt int2hex.txt (795 Bytes, 0 views)

Last edited by shabbir; 30Dec2012 at 09:12.. Reason: Code blocks