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..
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); }
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"); }
//Decimal to Hex value main() { char hex[16]={"0123456789ABCDEF"}; int num,i=0,j; char result[]; printf("Enter the Decimal No.\n"); scanf("%d",&num); while(num) { 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; [*] }
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.
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
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"); } ~
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]; } }