This post contains a total of 35+ Hand-Picked C Binary to Decimal Converters. All the Binary to Decimal Converter Examples are made using C Programming Language.
You can use the source codes of these programs for your own personal or educational use with credits to the original owner.
Related Posts
Click a Code to Copy it.
1. By Kyrillos Akram
Made by Kyrillos Akram. A small C Program to Convert Binary number to decimal. Change the value in “inBinarry(00001010))” to your own binary value. ( Source )
#include <stdio.h>
#define inBinarry(x) 0b##x
int main()
{
printf ("(00001010)2= (%d)10 ",inBinarry(00001010));
return 0;
}
2. By Saif Ali
Made by Saif Ali. ( Source )
#include <stdio.h>
int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); /* maximum five digits */
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
}
3. By Rakesh Gombi
Made by Rakesh Gombi. ( Source )
#include <stdio.h>
int main()
{ int n1, n,p=1;
int dec=0,i=1,j,d;
printf("Convert Binary to Decimal:\n ");
printf("-------------------------\n");
printf("Input a binary number :");
scanf("%d",&n);
n1=n;
for (j=n;j>0;j=j/10)
{
d = j % 10;
if(i==1)
p=p*1;
else
p=p*2;
dec=dec+(d*p);
i++;
}
printf("\nThe Binary Number : %d\nThe equivalent Decimal Number : %d \n\n",n1,dec);
}
4. By Kishore Kumar Biswas
Made by Kishore Kumar Biswas. ( Source )
#include <stdio.h>
#include <math.h>
int convertBinaryToDecimal(long long num);
int main() {
long long num;
printf("Enter a Binary number: ");
scanf("%lld",&num);
printf("%lld in binary = %d in decimal",num,convertBinaryToDecimal(num));
return 0;
}
int convertBinaryToDecimal(long long num){
int decimalNum=0,i=0,rem;
while(num !=0){
rem = num%10;
num /=10;
decimalNum += rem*pow(2,i);
i++;
}
return decimalNum;
}
5. By Zakaria Elalaoui
Made by Zakaria Elalaoui. ( Source )
#include<stdio.h>
#include<math.h>
long int bin2dec(long n){
int dec=0,i=0,rem;
while(n!= 0) {
rem=n%10;
n/=10;
dec+=rem*pow(2,i);
i++;
}
return dec;
}
int main()
{
long int bin;
scanf("%ld",&bin);
long int dec=bin2dec(bin);
printf("%ld",dec);
return 0;
}
6. By Aditya
Made by Aditya. Enter a binary number to get decimal number. ( Source )
#include<stdio.h>
int main()
{
int num,rem,dec=0,base=1,binary=0;
scanf("%d",&num);
while(num>0)
{
rem=num%10;
dec=dec+rem*base;
num=num/10;
base =base*2;
}
printf(" For binary Number the decimal no. is %d",dec);
}
7. By Prajwal C
Made by Prajwal C. ( Source )
#include <stdio.h>
int convert(int);
int main()
{
int dec,bin;
printf("Enter a binary number only:\n");
scanf("%d",&bin);
dec=convert(bin);
printf("The decimal equivalent of %d is %d.\n",bin,dec);
}
int convert(int bin)
{
if(bin==0)
{
return 0;
}
else
{
return(bin%10+2*convert(bin/10));
}
}
8. By Ritwick Raj Makhal
Made by Ritwick Raj Makhal. ( Source )
#include <stdio.h>
#include <math.h>
int binary2decimal(int n){
int decimal=0;
int x=0;
while(n>0){
decimal+=(n%10)*pow(2,x);
n/=10;
x++;
}
return decimal;
}
int main() {
int n;
scanf("%d",&n);
printf("%d",binary2decimal(n));
return 0;
}
9. By Zuhail
Made by Zuhail. ( Source )
//Did it at once //
//And feel free to share//
#include <stdio.h>
#include <math.h>
int main() {
int num;
int y=0;
int rem;
int digit=0;
long int deci=0;
int var;
scanf("%d",&num);
var = num;
while(num!=0)
{
rem = num % 10;
digit = rem*pow(2,y);
y++;
deci = deci + digit ;
num /= 10 ;
}
printf("\n %d in decimal is equal to %ld",var,deci);
return 0;
}
10. By G.Anjali Jha
Made by G.Anjali Jha. Simple C Program to convert Binary to decimal. ( Source )
#include <stdio.h>
int main()
{
int num,binary_val,decimal_val=0,base=1,rem;
printf("enter a binary number");
scanf("%d",&num);
binary_val=num;
while(num>0)
{
rem=num%10;
decimal_val=decimal_val+(rem*base);
num=num/10;
base=base*2;
}
printf("The binary number is=%d \n",binary_val);
printf("Its decimal eqivalent is =%d \n",decimal_val);
}
11. By ttsochat
Made by ttsochat. ( Source )
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int b;
printf("Give binary number.\n\n");
scanf("%d",&b);
int dec=0,i=0;
int a;
while(b>0)
{
a=b%10;
dec=dec+a*pow(2,i);
i++;
b=b/10;
}
printf("\nDecimal number is %d.\n",dec);
return 0;
}
12. By Dharshan2226
Made by Dharshan2226. ( Source )
// convert binary to decimal
#include <stdio.h>
#include <math.h>
// function prototype
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
13. By MemesrHeaven
Made by MemesrHeaven. Detects input whether its binary or not then converts it into it’s count part. ( Source )
#include<stdio.h>
#include<math.h>
int inbinary(int b, int m, int c);
int indecimal(int d, int m, int c);
void decision(int bd);
void main()
{
int bd;
printf("Enter a Binary or a Decimal number : ");
scanf("%d", &bd);
decision(bd);
}
void decision(int bd)
{
int m, c, b, d;
c = 0;
b = bd;
d = bd;
while(bd > 0)
{
m = bd % 10;
if((m != 1) && (m != 0))
{
c++;
break;
}
bd /= 10;
}
if(c > 0)
{
printf("Your input is a Decimal\n");
indecimal(d,m,c);
}
else
{
printf("Your input is a binary\n");
inbinary(b,m,c);
}
}
int inbinary(int b, int m, int c)
{
int p = 0, n = 2, dup;
m = 0;
c = 0;
dup = b;
while(b > 0)
{
m = b % 10;
c = c + m * pow(n,p);
p++;
b /= 10;
}
printf("The Conversion of (%d)B in Binary is (%d)D in Decimal\n", dup, c);
return 0;
}
int indecimal(int d, int m, int c)
{
int o, dup;
m = 1;
c = 0;
dup = d;
while(d != 0)
{
o = d % 2;
d /= 2;
c = c + o * m;
m *= 10;
}
printf("The Conversion of (%d)D is (%d)B in Binary\n", dup, c);
return 0;
}
14. By @noob_coder
Made by @noob_coder. ( Source )
#include <stdio.h>
#include <math.h>
int main() {
char n[10];
int s,sum=0,temp,t;
puts("Enter the Binary number:");
gets(n);
puts(n);
s=strlen(n);
temp=s-1;
for(int i=0;i<s;i++)
{
t=n[i]-48;
sum+=(t*pow(2,temp));
temp--;
}
puts("The Decimal Number is:");
printf("%d",sum);
return 0;
}
15. By Abhishek Singh Negi
Made by Abhishek Singh Negi. ( Source )
//easy to understand codes.if you have any problem in understanding ask me in comments
#include <stdio.h>
int power(int x,int y)
{ int num=1;
for(int i=1;i<=y;i++)
num=num*x;
return num;
}
int main() {
unsigned long long a;
int k=0;
int n=0;
unsigned long long sum=0;
int flag=0;
scanf("%llu",&a);
unsigned long long x=a;
while(a!=0)
{ n=a%10;
if(n==1||n==0)
{sum=sum+(n*power(2,k));
k++;
a=a/10;}
else
{flag=1;
break; }
}
if(flag==0)
printf("the decimal of %llu is %llu ",x,sum);
else
printf("flase input");
return 0;
}
16. By ABADA S
Made by ABADA S. ( Source )
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
void S(char in[],char s);
int contain(char in[],char s);
int main()
{
char b[100];//binary
puts("enter a number in binary system");
scanf("%s",b);
if(contain(b,'.')>0)
{
S(b,'.');
}
else if(contain(b,',')>0)
{
S(b,',');
}
else
{
int sum=0,n=0,i,j;
int b2[100];
for(j=0;j<100;j++)
b2[j]=0;
int i1;
for(i1=0;i1<100;i1++)
{
if(b[i1]=='0')
b2[n]=0;
else if(b[i1]=='1')
b2[n]=1;
else
break;
n++;
}n=0;
for(i=i1-1;i>=0;i--)
{
sum+=b2[i]*pow(2,n);
n++;
}
printf("%d",sum);
}printf("enjoy");
return 0;
}
void S(char in[],char s)
{
int s1=0,i,n=0;
float s2=0;
for(i=contain(in,s)-1;i>=0;i--)
{
if(in[i]=='1')
s1+=pow(2,n);
n++;
}//should be true
n=1;
for(i=contain(in,s)+1;i<strlen(in);i++)
{
if(in[i]=='1')
s2+=pow(2,-1*n);
n++;
}
float out=(float)s1+s2;
printf("%f\n",out);
}
int contain(char in[],char s)
{ int i;
for(i=0;i<strlen(in);i++)
{
if(in[i]==s)return i;
}return -1;
}
17. By jahendrie
Made by jahendrie. Convert binary numbers to decimal. ( Source )
/*******************************************************************************
* bin2dec.c | version 1.6 | FreeBSD License | 2018-02-12
* James Hendrie | [email protected]
*
* Description:
* Converts binary numbers to decimal integers
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_STRING_LENGTH 256
#define BIN_STR_LENGTH 7
#define VERSION "1.6"
/* -------------------- Global Options ----------------------- */
int bin2bin; // Binary output
int bin2dec; // Do binary to decimal conversion (default)
int bin2hex; // binary to hex
int bin2phex; // binary to precise hex
int bin2oct; // binary to octal
int sections; // print in 4-block sections if appropriate
int hexCaps;
int phexCaps;
int lineSpacing; // prettier output if desired
int verbose; // Verbosity
int textMode; // Text conversion mode
int bigEndian; // Read numbers as big-ending (default=1=yes)
int totalConversions; // Total number of conversions
static const char *optString = "bdvxXaAoslhteE";
/* Print the usage text */
void print_usage(FILE *fp)
{
fprintf( fp, "Usage:\tbin2dec [OPTIONS] BINARY_NUMBER[s]\n");
}
/* Print the help text */
void print_help(void)
{
/* Print usage */
print_usage(stdout);
/* Yeah, I know this is ugly. */
printf("\nThis program converts a binary number to a decimal number.\n" );
printf("\nOptions:\n");
printf(" -h or --help\tPrint this help text\n");
printf(" --version\tPrint version and author info\n");
printf(" -\t\tRead from stdin\n");
printf(" -v\t\tEnable verbose output (print what type each result is)\n");
printf(" -d\t\tDecimal output (default)\n");
printf(" -b\t\tBinary output\n");
printf(" -o\t\tOctal output\n");
printf(" -x\t\tHexadecimal output\n");
printf(" -X\t\tHex output with capital letters\n");
printf(" -a\t\tPrecise hex output (printf %%a, see 'man 3 printf')\n");
printf(" -A\t\tSame, but with capital letters\n");
printf(" -s\t\tOutput in 4-character sections, space-separated\n");
printf(" -l\t\tPrint a new line between sections of text\n");
printf(" -t\t\tText conversion mode\n");
printf(" -e\t\tRead binary numbers as little-endian\n" );
printf(" -E\t\tRead binary numbers as big-endian (default)\n" );
}
/* version and author info */
void print_version(void)
{
printf("bin2dec version %s\n", (VERSION) );
printf("James Hendrie - [email protected]\n");
}
/* Memory error; *description is sent from calling function */
void mem_error( const char *description )
{
fprintf(stderr, "ERROR: Out of memory\n");
fprintf(stderr, "%s\n", description);
}
/*
* Copies one string into another one (dest being presumably empty), minus
* spaces and newlines
*/
void strip_whitespace( char *destString, char *fromString )
{
int sLen = strlen( fromString );
int dCounter, fCounter;
for( dCounter = 0, fCounter = 0; fCounter <= sLen; ++fCounter )
{
if( fromString[ fCounter ] != ' ' && fromString[ fCounter ] != '\n' )
{
destString[ dCounter ] = fromString[ fCounter ];
++dCounter;
}
}
}
/* Check to make sure that the string has nothing but binary chars (1 or 0) */
int check_for_binary( char *s )
{
int i = 0;
for( i = 0; i < strlen(s); ++i )
{
/* Check for nulls or linefeeds */
if( s[i] == '\0' || s[i] == '\n' )
break;
/* Run through the string, make sure all chars are either 0 or 1 */
if( s[i] != '0' && s[i] != '1' )
{
return(1);
}
}
return(0);
}
/* Multiply 'number' to the power of 'power' */
double power_of( int number, int power )
{
/* If we're multiplying to the power of zero */
if( power == 0 )
return(1);
double current = number;
/* For loop; make my number, GROW!!!! */
int temp = 0;
for( temp = 1; temp < power; ++temp )
{
current = current * number;
}
/* Return finished number */
return( current );
}
/*
* Take a string (char *s), which has previously been verified to contain only
* binary characters (1 or 0), and convert it to a decimal number
*/
double binary_to_decimal( char *s )
{
/* Our initial variables */
unsigned int placement = 0;
double total = 0;
/* We're starting on the 'right side' of the binary string */
int i = 0;
// If we're doing the big-endian thing (default)
if( bigEndian == 1 )
{
for( i = strlen(s); i >= 0; --i )
{
/* If we hit a newline or a null char, just ignore it */
if( s[i] == '\n' || s[i] == '\0' )
continue;
if( s[i] == '0' ) { ++placement; }
else if( s[i] == '1' )
{
total += power_of( 2.0f, placement );
++placement;
}
} // for i, etc.
} // if bigEndian
// Otherwise, do the weird, little-endian stuff
else
{
for( i = 0; i < strlen( s ); ++i )
{
/* If we hit a newline or a null char, just ignore it */
if( s[i] == '\n' || s[i] == '\0' ) continue;
if( s[i] == '0' ) { ++placement; }
else if( s[i] == '1' )
{
total += power_of( 2.0f, placement );
++placement;
}
} // for i, etc.
}
/* Return our total */
return( total );
}
/* Prints our mostly-finished number string */
void print_number_string( char *s )
{
/* If we want to print in sections */
if( sections != 0 )
{
int i = 0; // Counter
int spaces = 0; // Number of spaces 'to the right' we've moved
/* Find and print the number of zeroes we need to pad */
for( i = 0; i < ( 4 - strlen(s) % 4 ); ++i )
{
printf("0");
++spaces;
}
/* Print the string char by char, add spaces when necessary */
for( i = 0; i < strlen(s); ++i )
{
if( spaces == 4 )
{
printf(" ");
spaces = 0;
}
printf("%c", s[i]);
++spaces;
}
printf("\n"); // Finish with a newline
}
else // Otherwise, just print the damn string
{
printf("%s\n", s);
}
}
/*
* Master output decider function thingy
*/
int string_printer( double *decimalResult )
{
/* Create a string pointer, allocate memory to it */
char *s = NULL;
s = malloc( MAX_STRING_LENGTH );
if( s == NULL )
{
mem_error( "string_printer: Cannot allocate memory for string" );
return(1);
}
/* Print the result */
if( bin2dec == 1 ) // Decimal
{
if( verbose == 1 )
printf( "DEC\t");
sprintf( s, "%s%0.lf", s, *decimalResult );
print_number_string( s );
memset( s, '\0', MAX_STRING_LENGTH );
}
if( bin2oct == 1 ) // Octal
{
if( verbose == 1 )
printf("OCT\t");
sprintf( s, "%s%o", s, (unsigned int)*decimalResult );
print_number_string( s );
memset( s, '\0', MAX_STRING_LENGTH );
}
if( bin2hex == 1 ) // Hex
{
if( verbose == 1 )
printf("HEX\t");
sprintf( s, "%s%x", s, (unsigned int)*decimalResult );
print_number_string( s );
memset( s, '\0', MAX_STRING_LENGTH );
}
if( hexCaps == 1 ) // Hex (with caps)
{
if( verbose == 1 )
printf("HEX\t");
sprintf( s, "%s%X", s, (unsigned int)*decimalResult );
print_number_string( s );
memset( s, '\0', MAX_STRING_LENGTH );
}
if( bin2phex == 1 ) // SUPER HEX
{
if( verbose == 1 )
printf("0xHEX\t");
sections = 0; // Need to do this, otherwise stuff gets ugly
sprintf( s, "%s%a", s, *decimalResult );
print_number_string( s );
memset( s, '\0', MAX_STRING_LENGTH );
}
if( phexCaps == 1 ) // SUPER HEX (with caps)
{
if( verbose == 1 )
printf("0xHEX\t");
sections = 0; // Need to do this, otherwise stuff gets ugly
sprintf( s, "%s%A", s, *decimalResult );
print_number_string( s );
memset( s, '\0', MAX_STRING_LENGTH );
}
/* free our string buffer */
free( s );
s = NULL;
return(0);
}
/*
* Here, we put the finishing touches on our line
*/
char* text_mode_printer_finish( char *line )
{
if( lineSpacing == 0 && totalConversions > 1 )
sprintf( line, "%s\t", line );
else if( totalConversions > 1 )
sprintf( line, "%s\n", line );
return( line );
}
/*
* Herein we compile a string of text to be printed, based on various factors
* such as whether or not verbosity is switched on, which conversions were
* called for, etc.
*/
void text_mode_printer( int ch, char *string )
{
char *line = malloc( 32 );
if( line == NULL )
{
mem_error("In: text_mode_printer");
exit(1);
}
if( verbose == 1 )
{
if( bin2bin == 1 )
{
sprintf( line, "%c BIN %s", ch, string );
text_mode_printer_finish( line );
printf("%s", line);
memset( line, '\0', 32 );
}
if( bin2oct == 1 )
{
sprintf( line, "%c OCT %o", ch, ch );
text_mode_printer_finish( line );
printf("%s", line);
memset( line, '\0', 32 );
}
if( bin2dec == 1 )
{
sprintf( line, "%c DEC %d", ch, ch );
text_mode_printer_finish( line );
printf("%s", line);
memset( line, '\0', 32 );
}
if( bin2hex == 1 )
{
sprintf( line, "%c HEX %x", ch, ch );
text_mode_printer_finish( line );
printf("%s", line);
memset( line, '\0', 32 );
}
if( hexCaps == 1 )
{
sprintf( line, "%c HEX %X", ch, ch );
text_mode_printer_finish( line );
printf("%s", line);
memset( line, '\0', 32 );
}
}
else
{
sprintf( line, "%c", ch );
text_mode_printer_finish( line );
printf("%s", line );
}
free( line );
line = NULL;
}
/*
* Convert a binary number (string) to a series of characters
*/
int number_to_string( char *string )
{
int sLen = strlen( string ); // Length of original string
int ch = 0; // Integer to receive character value
int stringMoved = 0; // Number of times string was incremeneted
int counter; // Generic counter
/* Create our bin string */
char *binStr = malloc( (BIN_STR_LENGTH) );
if( binStr == NULL )
{
mem_error("In: number_to_string");
return(1);
}
/* A mutable copy of the original string */
char fromStringStack[ sLen ];
char *fromString = fromStringStack;
/* Null out strings */
memset( binStr, '\0', (BIN_STR_LENGTH) );
memset( fromString, '\0', ( sLen + 1 ));
/* Strip whitespace */
strip_whitespace( fromString, string );
/*
* Go through the string, grabbing 8-bit (or fewer) chunks of it and
* converting said 'chunks' to ASCII character values, printing each
* along the way.
*/
while( 1 )
{
/* Check if we're only working with one character */
if( sLen <= (BIN_STR_LENGTH) + 1)
{
ch = (int)binary_to_decimal( fromString );
if( ch != 0 )
{
text_mode_printer( ch, fromString );
if( lineSpacing == 1 && totalConversions < 2 )
printf("\n");
}
break;
}
else
{
for( counter = 0; counter <= (BIN_STR_LENGTH) ; ++counter )
{
binStr[counter] = fromString[counter];
}
fromString += counter;
stringMoved += counter;
sLen -= counter;
ch = (int)binary_to_decimal( binStr );
text_mode_printer( ch, binStr );
memset( binStr, '\0', (BIN_STR_LENGTH) );
if( lineSpacing == 1 )
printf("\n");
else if( verbose == 1 )
printf("\t");
}
}
if( lineSpacing == 0 )
printf("\n");
/* Reset the original string */
fromString -= stringMoved;
/* Free / null binStr */
free( binStr );
binStr = NULL;
return(0);
}
int send_string_stdin(void)
{
char *theNumber = NULL; // Used for strtok
char line[256];
double decimalResult = 0;
int pCount = 0;
while( fgets(line, 256, stdin) != NULL )
{
/*
* We'll use strtok to tokenize the string, number by number, using the
* space character as a delimiter
*/
theNumber = strtok(line, " ");
while( theNumber != NULL )
{
/* If the user wants slightly prettier output */
if( lineSpacing == 1 && pCount > 0 && textMode == 0)
{
printf("\n");
}
if( textMode == 1 )
number_to_string( theNumber );
else
{
decimalResult = binary_to_decimal( theNumber );
string_printer( &decimalResult );
}
theNumber = strtok(NULL, " "); // Re-run strtok, grab next number
++pCount;
}
}
return(0);
}
/*
* ==========================================================================
* MAIN FUNCTION
* ==========================================================================
*/
int main( int argc, char *argv[] )
{
if( argc > 1 )
{
/* If they want help */
if( strcmp( "-h", argv[1]) == 0 || strcmp( "--help", argv[1]) == 0 )
{
print_help();
return(0);
}
/* If they want version / author info */
if( strcmp( "--version", argv[1]) == 0 )
{
print_version();
return(0);
}
}
/* Initialize variables */
bin2bin = 0;
bin2dec = 0;
bin2hex = 0;
bin2phex = 0;
bin2oct = 0;
sections = 0;
hexCaps = 0;
phexCaps = 0;
verbose = 0;
textMode = 0;
bigEndian = 1;
/* Get options */
int opt = getopt( argc, argv, optString );
while ( opt != -1 )
{
switch( opt )
{
case 'h': // Help
print_help();
return(0);
break;
case 'b': // Binary
bin2bin = 1;
break;
case 'd': // Decimal
bin2dec = 1;
break;
case 'v': // Verbosity
verbose = 1;
break;
case 'x': // Hexadecimal
bin2hex = 1;
break;
case 'X': // Hexadecimal with caps
hexCaps = 1;
break;
case 'a': // Extra precise hexadecimal
bin2phex = 1;
break;
case 'A': // Same, but also with caps
phexCaps = 1;
break;
case 'o': // Octal
bin2oct = 1;
break;
case 's': // Use sections
sections = 4;
break;
case 'l': // Print newlines between sections of output
lineSpacing = 1;
break;
case 't':
textMode = 1;
break;
case 'e':
bigEndian = 0;
break;
case 'E':
bigEndian = 1;
break;
default:
// Do nuttin'
break;
}
opt = getopt( argc, argv, optString );
}
/* Shift / adjust argc and argv */
argv += ( optind - 1 );
argc -= ( optind - 1 );
/* With no args, we default to decimal */
if( bin2dec == 0 && bin2hex == 0 && bin2phex == 0 && bin2oct == 0 &&
hexCaps == 0 && phexCaps == 0 && bin2bin == 0 )
{
bin2dec = 1;
}
/* Set the total number of conversions */
totalConversions = ( bin2dec + bin2hex + bin2phex + bin2oct +
hexCaps + phexCaps + bin2bin );
/* If they're reading from stdin */
if( argc == 1 || (strcmp( "-", argv[1] ) == 0) )
{
/* Grab string */
if( send_string_stdin() == 1 )
return(1);
else
return(0);
}
int pCount = 0; // Random counter!
while( argc > 1 )
{
if( textMode == 1 )
{
number_to_string( argv[1] );
--argc;
++argv;
++pCount;
continue;
}
/* Check to make sure it's a binary number */
if( check_for_binary( argv[1] ) != 0 )
{
/* If it isn't, tell them but don't kill the program */
fprintf(stderr, "WARNING: Not a binary number:\t%s\n", argv[1] );
--argc;
++argv;
++pCount;
continue;
}
/* Get our decimal number from the binary to decimal converter */
double decimalResult = binary_to_decimal( argv[1] );
/* If the user wants prettier output, give it to them */
if( lineSpacing == 1 && pCount > 0 )
printf("\n");
/* Handle binary output */
if( bin2bin == 1 )
{
if( verbose == 1 )
printf( "BIN\t" );
print_number_string( argv[1] );
}
/* Print that sumbitch out */
if( string_printer( &decimalResult ) == 1 )
return(1);
/* Increment or decrement our various lists and counters */
--argc;
++argv;
++pCount;
}
return(0);
}
18. By Appu
Made by Appu. ( Source )
#include <stdio.h>
int bintodec(int num);
void main()
{
int binarynum,decimalnum;
printf("enter the binary number \n");
scanf("%d",&binarynum);
decimalnum=bintodec(binarynum);
printf("the equivalent decimal no of %d is %d\n",binarynum,decimalnum);
}
int bintodec(int num)
{
if(!(num/10))
return(num);
else
return(num%10+bintodec(num/10)*2);
}
19. By AdilsonFuxe
Made by AdilsonFuxe. Iterative Binary to Decimal. ( Source )
#include <stdio.h>
int binarieToDecimal(int n);
int main()
{
int num, result;
printf("\n<ENTER> a number.: ");
scanf("%d", &num);
result = binarieToDecimal(num);
printf("\n%d = %d\n", num, result);
return 0;
}
int binarieToDecimal(int n)
{
int base = 1, part = 0, result = 0;
while (n)
{
part = n % 10;
n /= 10;
result = result + part * base;
base *= 2;
}
return result;
}
20. By AdilsonFuxe
Made by AdilsonFuxe. Recursive Binary to Decimal. ( Source )
#include <stdio.h>
int binarieToDecimal(int n, int base);
int main()
{
int num, result;
printf("\n<ENTER> a number.: ");
scanf("%d", &num);
result = binarieToDecimal(num, 1);
printf("\n%d = %d\n", num, result);
return 0;
}
int binarieToDecimal(int n, int base)
{
if (!n)
return 0;
return binarieToDecimal(n / 10, base * 2) + (n % 10) * base;
}
21. By Kalpana Sharma
Made by Kalpana Sharma. ( Source )
/*...Enter Binary No.
.
.
. ...Hope you like it ❤️*/
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
int decimalnum = 0, temp = 0, remainder;
while (binarynum!=0)
{
remainder = binarynum % 10;
binarynum = binarynum / 10;
decimalnum = decimalnum + remainder*pow(2,temp);
temp++;
}
return decimalnum;
}
int main()
{
long binarynum;
printf("Enter a binary number: ");
scanf("%ld", &binarynum);
printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
return 0;
}
22. By T Harish Kumar
Made by T Harish Kumar. ( Source )
#include<stdio.h>
#include<math.h>
int main()
{
int i=0,j=0,b[100],a=0,n,m=0,c=0;
//printf("Enter Binary Number");
scanf("%d",&n);
a=n;
if(a==1)
{
printf("%d",1);
}
else
{
while(a!=1)
{
b[i]=a%10;
a/=10;
i++;
m++;
}
b[i]=a;
i=0;
printf("Decimal Number:");
for(j=0;j<=m;j++)
{
c+=(pow(2,j)*b[i]);
i++;
}
printf("%d",c);
}
return 0;
}
23. By Donut
Made by Donut. ( Source )
#include <stdio.h>
#include <math.h>
int main() {
int bin;
scanf("%d",&bin);
int b=bin,count=-1,dec=0,dig;
while(b!=0){
count++;
dig=b%10;
dec+=dig*pow(2,count);
b/=10;
}
printf("%d",dec);
return 0;
}
24. By Venkataramulu Yeruva
Made by Venkataramulu Yeruva. ( Source )
// Created by Venkata Siva Battu
#include <stdio.h>
int main()
{
int n,x,d,b,i=-1,j,sum,a[100];
printf("enter a number \t");
scanf("%d",&n);
printf("%d\n",n);
b=1;
while(n>0)
{
x=n%10;
d=x*b;
n=n/10;
b=b*2;
i++;
a[i]=d;
}
printf("its binary form is\n");
sum=0;
for(j=0;j<=i;j++)
{
sum=sum+a[j];
}
printf("%d",sum);
return 0;
}
25. By Looper🇮🇳
Made by Looper🇮🇳. ( Source )
//Binary to Decimal.
#include<stdio.h>
#include<math.h>
int binary_decimal(int n);
int main()
{
int n;
printf("Enter Binary number : ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
return 0;
}
//Function to convert binary to decimal.
int binary_decimal(int n)
{
int decimal = 0, i = 0, rem;
while (n != 0)
{
rem = n % 10;
n /= 10;
decimal += rem * pow(2, i);
++i;
}
return decimal;
}
26. By Vikash
Made by Vikash. ( Source )
#include<stdio.h>
#include<math.h>
int main(){
long int i,n,x=0,a;
printf("Enter any binary number: ");
scanf("%ld",&n);
printf("\n The decimal conversion of %ld is ",n);
for(i=0;n!=0;++i)
{
a=n%10;
x=(a)*(pow(2,i))+x;
n=n/10;
}
printf("%ld",x);
return 0;
}
27. By amartyakumarsaha
Made by amartyakumarsaha. Basic Binary to Decimal converter using C. ( Source )
#include<stdio.h>
void binaryTodecimal();
int main(void)
{
int n;
printf("Enter a binary number :-");
scanf("%d",&n);
binaryTodecimal(n);
return 0;
}
void binaryTodecimal(int n)
{
int last_digit,temp,decimal=0,base=1;
temp=n;
while(temp)
{
last_digit=temp%10;
decimal+=last_digit*base;
base=base*2;
temp=temp/10;
}
printf("The Decimal Value Of %d is :- %d",n,decimal);
}
28. By Anjali Keshri
Made by Anjali Keshri. ( Source )
#include <stdio.h>
#include <math.h>
int main() {
int n,decimal=0,rem,i=0;
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
n=n/10;
decimal=decimal+rem*pow(2,i);
i++;
}
printf("%d",decimal);
return 0;
}
29. By Ricky Perwira Tanzil
Made by Ricky Perwira Tanzil. ( Source )
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
char a[50];
int c[50];
int e=0;
printf("THIS PROGRAM TO CONVERT BINARY TO INT DECIMAL\n");
printf("INPUT THE BINARY: ");
gets(a);
int b=strlen(a);
for(int i=0;i<b;i++){
if(a[i]=='0'){
c[i]=0;
}
if(a[i]=='1'){
c[i]=1;
}
}
for(int d=0;d<strlen(a);d++){
e+=(c[d]*pow(2,b-1));
b--;
}
printf("THE DECIMAL IS %d",e);
return 0;
}
30. By Supd
Made by Supd. ( Source )
#include<stdio.h>
#include<math.h>
int main()
{
int i=0,a=0,b=0,c,num;
printf("Enter any number in binary form : ");
scanf("%d",&num);
while(num!=0)
{
c=num%10;
if(c>1)
{
printf("\nEnter number in right format");
a=1;
}
b+=c*pow(2,i);
i++;
num/=10;
}
if(a==1){}
else
{
printf("\nDecimal number : %d",b);
}
return 0;
}
31. By muthu kumar
Made by muthu kumar. ( Source )
#include <stdio.h>
#include <math.h>
int main() {
int n, i=0 , r, p, s = 0;
scanf("%d",&n);
while(n!=0)
{
r = n%10;
if(r>1)
{
printf("Enter Correct Binary number");
return 0;
}
p = pow(2,i);
s = s + p*r;
i++;
n/=10;
}
printf("%d",s);
return 0;
}
32. By Bhavani V
Made by Bhavani V. ( Source )
#include <stdio.h>
#include<math.h>
int main() {
int n,i=0,r,p,s=0;
scanf("%d",&n);
while(n!=0)
{
r=n%10;
p=pow(2,i);
s=s+p*r;
i++;
n=n/10;
}
printf ("%d",s);
return 0;
}
33. By Lihsin Chen
Made by Lihsin Chen. ( Source )
#include <stdio.h>
#include <math.h>
int BinaryDecimal(int*,int,int*);
int main(){
int input;
int x[20];
scanf("%d",&input);
int i= input,j=0;
int decimal=0;
while(i>0){
x[j]=i%10;
i=i/10;
j++;
}
printf("%d",BinaryDecimal(x,j-1,&decimal));
return 0;
}
int BinaryDecimal(int* x,int j,int* decimal){
if(x[j]==1){
*decimal=pow(2,j)+*decimal;
}
if(j==0){
return *decimal;
}
BinaryDecimal(x,j-1,decimal);
}
34. By SANDEEP. K
Made by SANDEEP. K. ( Source )
#include <stdio.h>
#include <math.h>
int convertBinaryToDecimal(long long n);
int main()
{
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convertBinaryToDecimal(n));
return 0;
}
int convertBinaryToDecimal(long long n) { int decimalNumber = 0, i = 0, remainder; while (n!=0) { remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i); ++i;
}
return decimalNumber;
}
35. By SAURAV KUMAR
Made by SAURAV KUMAR. ( Source )
#include <stdio.h>
#include <math.h>
void BtoD (int B)
{
int rem,result =0,count=0;
for (int i=B;i>0;i=i/10)
{
rem =i%10;
result +=(rem * pow(2,count ));
count ++;
}
printf ("%d",result );
}
int main() {
int bno;
scanf("%d",&bno);
printf ("Decimal equivalent of %d:\n",bno);
BtoD(bno);
return 0;
}
36. By Alejandro Rodríguez
Made by Alejandro Rodríguez. Choose what you want to do, binary to decimal or vice-versa. ( Source )
#include<stdio.h>
#include<math.h>
#include<string.h>
void decbin();
void bindec();
int main()
{
int opc;
printf("\n\n\tChoose an option:\n\t1. Decimal to binary\t2. Binary to decimal\n");
scanf("%i",&opc);
switch(opc)
{
case 1:
decbin();
break;
case 2:
bindec();
break;
}
return 0;
}
void decbin()
{
int bin[13], i=0, j=0, dec;
inicio:
puts("\n\tEnter a decimal number:\t");
scanf("%d",&dec);
if(dec<=4096) // Menor o igual al maximno numero para 12 bits 2^12= 4096
do {
bin[i]=dec%2;//Genera los digitos en binario
dec/=2;
i++;
}while(dec!=0);
else
{
printf("\n\tNumber greater than 12 bits");
goto inicio;
}
printf("\n\tIn binary is:\t");
for(j=i-1; j>=0; j--)
printf("%i", bin[j]);
}
void bindec()
{
char cad[12];
inicio:
puts("\n\tEnter a binary number:\n ");
scanf("%s", cad);
int n, i, k, dec=0;
n=strlen(cad);
if(n>12)
{
printf("\n\tNumber greater than 12 bits" );
goto inicio;
}
for(i=0, k=n-1;i<n; i++, k--)
{
if(cad[i]!='1'&&cad[i]!='0')
{
printf("\n\tNumber is not binary" );
goto inicio;
}
else
if(cad[i]=='1')
dec+=(pow(2,k));
}
printf("\n\tIn decimal is:\t%d",dec);
}