| | |
Binary to Decimal Conversion
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
The computer is a binary beast. We want to go from the one-fingered digital box to the ten-fingered human being and convert binary to denary(base 10) numbers. This is how it works, for instance, binary 10010 is calculated as 1*16+0*8+0*4+1*2+0*1 = decimal 18. Just because it's easy, let's throw in hexadecimal and octal results too. The added bonus is the almost foolproof entry of data.
// Binary to Decimal, Hexadecimal and Octal conversion program // tested with Pelles C vegaseat 15dec2004 #include <stdio.h> #include <string.h> int bin2dec(char *bin); int main() { char bin[80] = ""; char *p; int dec; while(strcmp(bin,"0")) { printf("\n Enter a binary number (just 0 to EXIT): "); 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 Hexadecimal = 0x%04X Octal = 0%o\n",dec,dec,dec); } 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"); return (0); } for(b = 1, m = len; m > k; m--) { // 1 2 4 8 16 32 64 ... 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); }
0
•
•
•
•
test.c 11 error [Warning 603] Symbol 'bin' (line 8) not initialized
test.c 14 error [Warning 534] Ignoring return value of function 'gets(char *)'
test.c 14 error [Warning 421] Caution -- function 'gets(char *)' is considered dangerous
test.c 17 error [Info 725] Expected positive indentation from line 16
test.c 19 error [Warning 534] Ignoring return value of function '_fgetc(struct {...} *)'
test.c 29 error [Info 713] Loss of precision (assignment) (unsigned int to int)
test.c 47 error [Info 818] Pointer parameter 'bin' (line 24) could be declared as pointing to const
test.c 14 error [Warning 534] Ignoring return value of function 'gets(char *)'
test.c 14 error [Warning 421] Caution -- function 'gets(char *)' is considered dangerous
test.c 17 error [Info 725] Expected positive indentation from line 16
test.c 19 error [Warning 534] Ignoring return value of function '_fgetc(struct {...} *)'
test.c 29 error [Info 713] Loss of precision (assignment) (unsigned int to int)
test.c 47 error [Info 818] Pointer parameter 'bin' (line 24) could be declared as pointing to const
0
•
•
•
•
Well you can read this why gets is bad
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
0
•
•
•
•
It was a linter, by the way.
scanf("%s",bin);
This is just a diguised version of gets(). It suffers the same ills.
". Written by some real winner named HAMMER."
...who apparently still knows more than you.
scanf("%s",bin);
This is just a diguised version of gets(). It suffers the same ills.
". Written by some real winner named HAMMER."
...who apparently still knows more than you.
0
•
•
•
•
A slight modification for the bin2dec function. Removes the nested for loop. Reduces the overhead and removes the temporary variable 'm'.
int bin2dec(char *bin)
{
int b , k, n;
int len, sum = 0;
len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
b = 1;
n = (bin[k] - '0'); // char to numeric value
if ((n > 1) || (n < 0))
{
puts("\n\n ERROR! BINARY has only 1 and 0!\n");
return (0);
}
b = b<<(len-k);
// sum it up
sum = sum + n * b;
//printf("%d*%d + ",n,b); // uncomment to show the way this works
}
return(sum);
} Last edited by niek_e; 33 Days Ago at 2:34 pm. Reason: Added code-tags
Similar Threads
- Code Snippet: Decimal to Binary conversion (C)
- binary to decimal conversion!! (C++)
- decimal to binary conversion (C++)
- decimal to binary conversion (C++)
- Help! Decimal to binary conversion (C)
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi



