code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//     

//program bintodec.c

//--------------------------------------

//     ----------------------------------------

//     

// Includes

//--------------------------------------

//     ----------------------------------------

//     

#include 

//--------------------------------------

//     ----------------------------------------

//     

// Prototypes

//--------------------------------------

//     ----------------------------------------

//     

int bin_to_dec(char binstr[ ]);

//--------------------------------------

//     ----------------------------------------

//     

// Main

//--------------------------------------

//     ----------------------------------------

//     

int main(void)

    {

     char binstring[ ] = "01000001";

     int retvalue;

     retvalue = bin_to_dec(binstring);

     printf("%s = %d\n", binstring, retvalue);

     return 0;

}

//--------------------------------------

//     ----------------------------------------

//     

// Functions

//--------------------------------------

//     ----------------------------------------

//     

int bin_to_dec(char binstr[ ])

    {

     int loop, loop2, loop3, dec, numarray[8];

     for (loop=0; loop<8; loop++)

         {

         if (binstr[loop] == '1')

             {

             if (loop == 7)

             numarray[loop] = 1;

             else

                 {

                 numarray[loop] = 2;

                 for (loop2=7-loop; loop2>1;loop2--)

                 numarray[loop] = numarray[loop] * 2;

                 }

                 }

                 else if (binstr[loop] == '0')

                     {

                     numarray[loop] = 0;

                     }

                     else

                     return -1;

                     }

                     dec = numarray[0] + numarray[1] + numarray[2] + numarray[3] + numarray[4]

                     + numarray[5] + numarray[6] + numarray[7];

                     return dec;

                }