| | |
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
![]() |
•
•
Join Date: Jan 2009
Posts: 10
Reputation:
Solved Threads: 0
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.
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.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int bin2dec(char *bin); int main() { char bin[9] = ""; char *p; int dec; while(strcmp(bin,"0")) { printf("\n Enter a binary number (just 0 to EXIT): "); //Enter number to be converted in binary form fgets(bin, sizeof(bin), stdin); // check for and remove trailing \n if ((p = strchr(bin,'\n')) != NULL) { *p = '\0'; } dec = bin2dec(bin); if (dec) printf("\nDecimal = %d \n",dec); //Display Converted # in Decimal, } getchar(); // wait return 0; } // convert a binary string to a decimal number, returns decimal value int bin2dec(char *bin) { int b, k, m, n; int len, sum = 0; len = strlen(bin) - 1; for(k = 0; k <= len; k++) { n = (bin[k] - '0'); // char to numeric value if ((n > 1) || (n < 0)) { puts("\n\n ERROR! BINARY has only 1 and 0!\n"); //Error if anything other than 1 or 0 entered return (0); } for(b = 1, m = len; m > k; m--) { // 1 2 4 8 16 32 64 128 ... place-values, reversed here b *= 2; // } // sum it up sum = sum + n * b; //printf("%d*%d + ",n,b); // uncomment to show the way this works } return(sum); }
Last edited by Ancient Dragon; Jan 13th, 2009 at 4:25 pm. Reason: add code tags
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:
Include <math.h>
and replace your the previously mentioned code with this:
c Syntax (Toggle Plain Text)
for(b = 1, m = len; m > k; m--) { // 1 2 4 8 16 32 64 128 ... place-values, reversed here b *= 2; } // sum it up sum = sum + n * b;
Include <math.h>
and replace your the previously mentioned code with this:
C Syntax (Toggle Plain Text)
sum += n * (int)pow(2, k);
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.
> 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.
•
•
Join Date: May 2008
Posts: 586
Reputation:
Solved Threads: 94
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...
I'm really hoping this wasn't homework, you might be hard-pressed to explain where the idea came from
c Syntax (Toggle Plain Text)
int bin2dec(char * bin) { int n; int sum = 0; while (*bin) { n = *bin - '0'; if (n < 0 || n > 1) { printf("\nInvalid binary character: '%c'\n", *bin); puts("Binary only uses the digits 0 and 1.\n"); return 0; } // double what we had before and add a new low bit sum = (sum << 1) + n; ++bin; } return sum; }
I'm really hoping this wasn't homework, you might be hard-pressed to explain where the idea came from
![]() |
Other Threads in the C Forum
- Previous Thread: Help with grep
- Next Thread: Finding two equal subsequences
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






