| | |
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; }
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Jun 2008
Posts: 143
Reputation:
Solved Threads: 12
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 10: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
Views: 13515 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch build c++ c/c++ char character cheating code coke command conversion convert copy database decimal directory download dude dynamic ebook error exec factorial fgets file fork function functions getline givemetehcodez grade graphics hardware homework i/o input insert int integer lazy line linked linkedlist linux list lists loop malloc matrix memory multi mysql no-effort numbers output path pointer pointers problem process program programming programs read readfile recursion recursive recv recvblocked reverse scan scanf sdl shape single socketprograming socketprogramming sockets sorting spoonfeeding stdin string strings strtok structures student system turbo-c txt unix user variable visualstudio voidmain() whythiscodecausesegmentationfault windows






