made this binart yo decimal converter and i want to make it smaller and more compact

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 10
Reputation: al3x748769 has a little shameless behaviour in the past 
Solved Threads: 0
al3x748769 al3x748769 is offline Offline
Newbie Poster

made this binart yo decimal converter and i want to make it smaller and more compact

 
-1
  #1
Jan 13th, 2009
hey,
i made this a few years ago. i want to take the error when u enter a different number and would like to make it more compact. can you guys help me make it smaller. this is a 8 bit binary to decimal converter.



  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5.  
  6.  
  7. int bin2dec(char *bin);
  8.  
  9.  
  10.  
  11. int main()
  12.  
  13. {
  14.  
  15. char bin[9] = "";
  16.  
  17. char *p;
  18.  
  19. int dec;
  20.  
  21.  
  22.  
  23. while(strcmp(bin,"0"))
  24.  
  25. {
  26.  
  27. printf("\n Enter a binary number (just 0 to EXIT): "); //Enter number to be converted in binary form
  28.  
  29. fgets(bin, sizeof(bin), stdin);
  30.  
  31. // check for and remove trailing \n
  32.  
  33. if ((p = strchr(bin,'\n')) != NULL)
  34.  
  35. {
  36.  
  37. *p = '\0';
  38.  
  39. }
  40.  
  41. dec = bin2dec(bin);
  42.  
  43. if (dec) printf("\nDecimal = %d \n",dec); //Display Converted # in Decimal,
  44.  
  45. }
  46.  
  47.  
  48.  
  49. getchar(); // wait
  50.  
  51. return 0;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. // convert a binary string to a decimal number, returns decimal value
  58.  
  59. int bin2dec(char *bin)
  60.  
  61. {
  62.  
  63. int b, k, m, n;
  64.  
  65. int len, sum = 0;
  66.  
  67.  
  68.  
  69. len = strlen(bin) - 1;
  70.  
  71. for(k = 0; k <= len; k++)
  72.  
  73. {
  74.  
  75. n = (bin[k] - '0'); // char to numeric value
  76.  
  77. if ((n > 1) || (n < 0))
  78.  
  79. {
  80.  
  81. puts("\n\n ERROR! BINARY has only 1 and 0!\n"); //Error if anything other than 1 or 0 entered
  82.  
  83. return (0);
  84.  
  85. }
  86.  
  87. for(b = 1, m = len; m > k; m--)
  88.  
  89. {
  90.  
  91. // 1 2 4 8 16 32 64 128 ... place-values, reversed here
  92.  
  93. b *= 2; //
  94.  
  95. }
  96.  
  97. // sum it up
  98.  
  99. sum = sum + n * b;
  100.  
  101. //printf("%d*%d + ",n,b); // uncomment to show the way this works
  102.  
  103. }
  104.  
  105. return(sum);
  106.  
  107. }
Last edited by Ancient Dragon; Jan 13th, 2009 at 4:25 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: made this binart yo decimal converter and i want to make it smaller and more compact

 
0
  #2
Jan 13th, 2009
Instead of this:
  1. for(b = 1, m = len; m > k; m--)
  2. {
  3. // 1 2 4 8 16 32 64 128 ... place-values, reversed here
  4. b *= 2;
  5. }
  6. // sum it up
  7. sum = sum + n * b;

Include <math.h>
and replace your the previously mentioned code with this:
  1. sum += n * (int)pow(2, k);
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: made this binart yo decimal converter and i want to make it smaller and more compact

 
0
  #3
Jan 14th, 2009
> and replace your the previously mentioned code with this:
> sum += n * (int)pow(2, k);
Bad idea.
a) it's slower
b) you're at the mercy of whatever inaccuracies are inherent in the floating point library.

> can you guys help me make it smaller.
Sure
a) use code tags - something you're consistently failing to do
b) strip out the excess of newlines
c) learn to indent code.
It's horrible.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 586
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 94
Murtan Murtan is offline Offline
Posting Pro

Re: made this binart yo decimal converter and i want to make it smaller and more compact

 
0
  #4
Jan 14th, 2009
Well, if you're not afraid of pointers...

  1. int bin2dec(char * bin)
  2. {
  3. int n;
  4. int sum = 0;
  5. while (*bin)
  6. {
  7. n = *bin - '0';
  8. if (n < 0 || n > 1)
  9. {
  10. printf("\nInvalid binary character: '%c'\n", *bin);
  11. puts("Binary only uses the digits 0 and 1.\n");
  12. return 0;
  13. }
  14. // double what we had before and add a new low bit
  15. sum = (sum << 1) + n;
  16. ++bin;
  17. }
  18. return sum;
  19. }

I'm really hoping this wasn't homework, you might be hard-pressed to explain where the idea came from
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC