| | |
convert Decimal to Hexadecimal system..
![]() |
hi eveRyone,
I just want to have your tips for my code..
I want to print out the numbers 10-16 in letters like this, 10=A,11=B,12=C,13=D,14=E,15=F,16=G..but i dont know how to make it coz im new with C..
I just want to have your tips for my code..
C Syntax (Toggle Plain Text)
#include <stdio.h> main() { hexa = 16; printf("enter decimal number: "); scanf("%d",&deci); for (ctr = 1; ctr<=deci; ctr++) quotient = deci / hexa; printf("Equivalent in Hexadecimal is %d",quotient); getche(); }
I want to print out the numbers 10-16 in letters like this, 10=A,11=B,12=C,13=D,14=E,15=F,16=G..but i dont know how to make it coz im new with C..
the easiest way is to use sprintf with "%X" format specifier
C Syntax (Toggle Plain Text)
int main() { char buf[255] = {0}; int x = 1234; sprintf(buf,"%X", x); printf("%s\n", buf); return 0; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 7
You made me laugh with your post to convert decimal to hex, including "A,B...G"
*G* ??
Anyway, your algorithm to change decimal to hexadecimal is wrong. Here's a modded version of your program, which shows what the hex value of a decimal, should be:
*G* ??

Anyway, your algorithm to change decimal to hexadecimal is wrong. Here's a modded version of your program, which shows what the hex value of a decimal, should be:
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int hexa = 16, deci, quotient, ctr; printf("enter decimal number: "); scanf("%d",&deci); for (ctr = 1; ctr<=deci; ctr++) quotient = deci / hexa; printf("Equivalent in Hexadecimal is %d",quotient); printf("\n\n Try this for Hexadecimal: %X", deci); getche(); return 0; } Good luck with your studies.
Not to be nitpicky, but getche() isn't standard C. You should use getchar() instead.
And here's a link about scanf() (or did I give you it before?)
And here's a link about scanf() (or did I give you it before?)
Thank you very much dude, I appreciate that!
Can you help me with my prob?
here's my code..
I want to print it out in binary, but the answer is being written out in reverse.
Can you help me with my prob?
here's my code..
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int ctr, bin, quotient, deci=0, binary = 2; float rem; char mark_magic; gotoxy(28,9);printf("enter decimal number: "); scanf("%d",&deci); quotient = deci / binary; printf("Equivalent in Binary is "); for(bin=0; bin <=3; bin++) { printf("%d",deci % 2); deci = deci / 2; } getchar(); }
Did you learn about arrays yet? You could store everything in an array and then print them out in reverse order.
You could also change the calculation. Look into the pow() function
You could also change the calculation. Look into the pow() function
C Syntax (Toggle Plain Text)
void printB( int num ) { if( num == 0 ) return; else { printB( num / 2 ); printf("%d", num % 2 ); } }
You could solve the problem of you binary values printing in reverse using the recursive function. Have a look the function above. You could see the before the function is called again it pushes the "num % 2" to the stack and when it return it print the values which is on top of the static that is "0%2" ........ "num%2".
Call the function as follow:
C Syntax (Toggle Plain Text)
printB( 23 ); /* my output 10111 */
NOTE: I wonder this would work for a long decimal number, due to overflow!
You might as well, look into some concept of code indendation, that code which you have posted has a horriable indendation! And it also give a me a nasty taste of you using a very old Turbo C compile??? Start looking at some better compiler. DEV-C++, Code::block
ssharish
Last edited by ssharish2005; Jul 23rd, 2008 at 11:40 pm.
![]() |
Similar Threads
- number system conversion (C++)
- i dont know how to continue... (C++)
- Anyone can help?? (C++)
- Cannot resolve symbol (Java)
Other Threads in the C Forum
- Previous Thread: Re: converting string[][] to float[]
- Next Thread: Plz give me code to find the squareroot of a number
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






