Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)

 owen29 17Mar2009 18:23

Decimal to Hexadecimal Conversion in C..

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

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..

Re: Decimal to Hexadecimal Conversion in C..

r[i]=n1%16;

what is n1 ?
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); }```

 rahulmaximus 4Jun2011 03:30

Re: Decimal to Hexadecimal Conversion in C..

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"); }```

 bmsangati 13Jun2012 13:28

Decimal to Hexadecimal Conversion in C..

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. }

 bmsangati 13Jun2012 13:29

Re: Decimal to Hexadecimal Conversion in C..

Hi friends, this is Decimal to Hex conversion

 shabbir 13Jun2012 13:31

Re: Decimal to Hexadecimal Conversion in C..

Quote:
 Originally Posted by bmsangati (Post 95699) 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.

 anishsina 5Jul2012 21:11

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

 debugEnthu 6Jul2012 12:43

Re: Decimal to Hexadecimal Conversion in C..

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"); }```
~

 C2E 30Dec2012 03:56

Re: Decimal to Hexadecimal Conversion in C..

1 Attachment(s)
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];   }  }```

 All times are GMT +5.5. The time now is 16:12.