943,095 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4314
  • C RSS
Jan 7th, 2010
-2

Hex to Decimal Conversion in C

Expand Post »
  1. #include <stdio.h>
  2. main()
  3. {
  4. int number;
  5. int ch;
  6. char sh[100];
  7. int i=0,j=0;
  8.  
  9. printf("input No in Hex: ");
  10. scanf("%s",&sh);
  11. number = 0;
  12. i =strlen(sh);
  13. j=0;
  14. while(i!=0)
  15. {
  16. ch = sh[j];
  17. printf("Ch:%d\t%d\t%d\n",ch,'7',ch-'7');
  18.  
  19. if(('0' <= ch && ch <= '9'))
  20. {
  21. number = number * 16;
  22. printf("Number :%d\n",number);
  23. number = number + (ch - '0');
  24. }
  25.  
  26. if(('A' <= ch && ch && ch <= 'Z'))
  27. {
  28. number = number * 16;
  29. printf("Number :%d\n",number);
  30. number = number + (ch - '7');
  31. }
  32.  
  33. printf("Number :%d\n",number);
  34. i--;
  35. j++;
  36.  
  37. }
  38.  
  39.  
  40. printf("The O/p No in Decimal is : %d\n",number);
  41.  
  42.  
  43. }
Posted by : sheik@coromandel
Last edited by adatapost; Jan 7th, 2010 at 10:37 pm. Reason: Added [code] tags. Encase your code in: [code] and [/code] tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mapla is offline Offline
3 posts
since Aug 2009
Jan 7th, 2010
0
Re: Hex to Decimal Conversion in C
It would be easier, and probably better, to use strtol, strtoul, or even sscanf, if you ask me.

Did you have a question about the code?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 10th, 2010
1

Hex to Dec Simpler method...

  1. #include<stdio.h>
  2. int main()
  3. {
  4. char sh[100] = "ED",*stop;
  5. int test;
  6.  
  7. test = strtol(sh,&stop,16);
  8. printf("Test:%d",test);
  9. }
Last edited by adatapost; Jan 10th, 2010 at 9:44 pm. Reason: Added [code] tags. Encase your code in: [code] and [/code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mapla is offline Offline
3 posts
since Aug 2009
Jan 11th, 2010
0
Re: Hex to Decimal Conversion in C
Click to Expand / Collapse  Quote originally posted by mapla ...
  1. #include<stdio.h>
  2. int main()
  3. {
  4. char sh[100] = "ED",*stop;
  5. int test;
  6.  
  7. test = strtol(sh,&stop,16);
  8. printf("Test:%d",test);
  9. }
Very nice. I assume it works now...
Moderator
Reputation Points: 3275
Solved Threads: 886
Posting Sage
WaltP is offline Offline
7,699 posts
since May 2006
Jan 11th, 2010
0
Re: Hex to Decimal Conversion in C
why not just use:
  1. printf("Input hexadecimal number: ");
  2. int hex;
  3. scanf("%x",hex);

Seems easiest to me, then it gets stored as an int. you can display it as a hex or a decimal:
printf("%X",hex) Hex
printf("%d",hex) Decimal
Reputation Points: 61
Solved Threads: 5
Junior Poster
prushik is offline Offline
101 posts
since Oct 2007

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: how to exit from the while loop ??
Next Thread in C Forum Timeline: Problem with fscanf float column read





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


Follow us on Twitter


© 2011 DaniWeb® LLC