943,910 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 42732
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 22nd, 2008
-1

convert Decimal to Hexadecimal system..

Expand Post »
hi eveRyone,
I just want to have your tips for my code..

  1.  
  2. #include <stdio.h>
  3.  
  4. main()
  5. {
  6. hexa = 16;
  7. printf("enter decimal number: ");
  8.  
  9. scanf("%d",&deci);
  10.  
  11. for (ctr = 1; ctr<=deci; ctr++)
  12.  
  13. quotient = deci / hexa;
  14.  
  15. printf("Equivalent in Hexadecimal is %d",quotient);
  16.  
  17.  
  18. getche();
  19. }

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..
Similar Threads
Reputation Points: 6
Solved Threads: 0
Newbie Poster
campuzcrazyness is offline Offline
17 posts
since Jan 2008
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

the easiest way is to use sprintf with "%X" format specifier
  1. int main()
  2. {
  3. char buf[255] = {0};
  4. int x = 1234;
  5. sprintf(buf,"%X", x);
  6. printf("%s\n", buf);
  7. return 0;
  8. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

wow! thanks a lot mr.,but still nothing happened..
Reputation Points: 6
Solved Threads: 0
Newbie Poster
campuzcrazyness is offline Offline
17 posts
since Jan 2008
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

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:

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int hexa = 16, deci, quotient, ctr;
  6. printf("enter decimal number: ");
  7. scanf("%d",&deci);
  8.  
  9. for (ctr = 1; ctr<=deci; ctr++)
  10. quotient = deci / hexa;
  11.  
  12. printf("Equivalent in Hexadecimal is %d",quotient);
  13. printf("\n\n Try this for Hexadecimal: %X", deci);
  14.  
  15. getche();
  16. return 0;
  17. }
  18.  
  19. Good luck with your studies.
Reputation Points: 416
Solved Threads: 181
Nearly a Posting Virtuoso
Adak is offline Offline
1,463 posts
since Jun 2008
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

thanKs duDe!
Reputation Points: 6
Solved Threads: 0
Newbie Poster
campuzcrazyness is offline Offline
17 posts
since Jan 2008
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

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?)
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

Thank you very much dude, I appreciate that!

Can you help me with my prob?
here's my code..

  1. #include <stdio.h>
  2.  
  3.  
  4. int main(void) {
  5.  
  6.  
  7. int ctr, bin, quotient, deci=0, binary = 2;
  8. float rem;
  9. char mark_magic;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. gotoxy(28,9);printf("enter decimal number: ");
  16.  
  17. scanf("%d",&deci);
  18. quotient = deci / binary;
  19.  
  20. printf("Equivalent in Binary is ");
  21.  
  22. for(bin=0; bin <=3; bin++)
  23. {
  24. printf("%d",deci % 2);
  25. deci = deci / 2;
  26. }
  27.  
  28.  
  29.  
  30. getchar();
  31. }
I want to print it out in binary, but the answer is being written out in reverse.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
campuzcrazyness is offline Offline
17 posts
since Jan 2008
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jul 23rd, 2008
0

Re: convert Decimal to Hexadecimal system..

  1. void printB( int num )
  2. {
  3. if( num == 0 )
  4. return;
  5. else
  6. {
  7. printB( num / 2 );
  8. printf("%d", num % 2 );
  9. }
  10. }

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:
  1. printB( 23 );
  2.  
  3. /* my output
  4. 10111
  5. */

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.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Jul 24th, 2008
0

Re: convert Decimal to Hexadecimal system..

Thanks everyone, I finally got it right!
Reputation Points: 6
Solved Threads: 0
Newbie Poster
campuzcrazyness is offline Offline
17 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: hex output of fwrite-ing char array
Next Thread in C Forum Timeline: Build Failed of C code by using Xcode





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC