The Open Source Swiss Army Knife

/programmingToolBox/comp_orgI/
/programmingToolBox/comp_orgI/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /programmingToolBox   /comp_orgI 
Permalink: bindec.c
Title: c program to convert binary to decimal
article options : please login   |  raw source view  

int Bin2Dec(char *);

int main(void) {
	char * str = "here is a line";
	char * str2 = (char *)malloc(sizeof(char) * 100);
	scanf("%99s", str2);	
	int converted = Bin2Dec(str2);
	printf("%d\n",converted);
}

int Bin2Dec(char *sBin)
{
    int n = 0;
    char *cp;

    for (cp = sBin; *cp != '\0' && (*cp == '1' || *cp == '0'); cp++)
    {
        n <<= 1;  /* left-shift by 1; equivalent to *=2 */
        if (*cp == '1')
            n += 1;
    }
    return n;
}

Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top