convert Decimal to Hexadecimal system..

Reply

Join Date: Jan 2008
Posts: 15
Reputation: campuzcrazyness is an unknown quantity at this point 
Solved Threads: 0
campuzcrazyness's Avatar
campuzcrazyness campuzcrazyness is offline Offline
Newbie Poster

convert Decimal to Hexadecimal system..

 
-1
  #1
Jul 22nd, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,339
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: convert Decimal to Hexadecimal system..

 
0
  #2
Jul 23rd, 2008
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. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: campuzcrazyness is an unknown quantity at this point 
Solved Threads: 0
campuzcrazyness's Avatar
campuzcrazyness campuzcrazyness is offline Offline
Newbie Poster

Re: convert Decimal to Hexadecimal system..

 
0
  #3
Jul 23rd, 2008
wow! thanks a lot mr.,but still nothing happened..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: convert Decimal to Hexadecimal system..

 
0
  #4
Jul 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: campuzcrazyness is an unknown quantity at this point 
Solved Threads: 0
campuzcrazyness's Avatar
campuzcrazyness campuzcrazyness is offline Offline
Newbie Poster

Re: convert Decimal to Hexadecimal system..

 
0
  #5
Jul 23rd, 2008
thanKs duDe!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,819
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: convert Decimal to Hexadecimal system..

 
0
  #6
Jul 23rd, 2008
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?)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: campuzcrazyness is an unknown quantity at this point 
Solved Threads: 0
campuzcrazyness's Avatar
campuzcrazyness campuzcrazyness is offline Offline
Newbie Poster

Re: convert Decimal to Hexadecimal system..

 
0
  #7
Jul 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,819
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: convert Decimal to Hexadecimal system..

 
0
  #8
Jul 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: convert Decimal to Hexadecimal system..

 
0
  #9
Jul 23rd, 2008
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: campuzcrazyness is an unknown quantity at this point 
Solved Threads: 0
campuzcrazyness's Avatar
campuzcrazyness campuzcrazyness is offline Offline
Newbie Poster

Re: convert Decimal to Hexadecimal system..

 
0
  #10
Jul 24th, 2008
Thanks everyone, I finally got it right!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC