////////////////////////////////////////////////////////////////////////////////
//                        Coypright (C) Aviral Mittal.
////////////////////////////////////////////////////////////////////////////////
//   All rights reserved. Reproducion in whole or in part is prohibited without 
//   written consent of copyright Owner.The professional use of this material 
//   is subject to the copy right owners approval in written. 
////////////////////////////////////////////////////////////////////////////////
//                   Comments welcome on avimit@yahoo.com
//                   Visit http://www.vlsiip.com for more free stuff
////////////////////////////////////////////////////////////////////////////////
// This program converts a binary number into integer
// The input given to this program will be a binary number like 11100101
// Output will be 
// Example: ./bin2int 10101010
// Above is an example, where the user is trying to convert 0.45823 into binary
// ./bin2point 10101010
//  
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
//Prototype Declaration
long int  po(short int x,short int y);
//Prototype Declaration Ends

//Variables Declaration
char error = 0; //Error Flag
int int_val ;
int poof2;
char len;
char i;
char instr[32];
char *pinstr;
//Variables Declaration Ends
int main(int argc, char* argv[]) {
//Initializations
  int_val = 0;
  poof2 = 0;
  len = 0;
  pinstr = &instr[0];
//Initializations Ends
  if(argc == 2) {
    error = 0;
////////////////////////////////////////////////////////////////////////////////
// Main Program Here
////////////////////////////////////////////////////////////////////////////////
    int_val = 0;
    while(*argv[1] != '\0') {
      if(*argv[1]-48 == 0 || *argv[1]-48 == 1) {
        *pinstr = *argv[1];
        //printf("Input char is %d\n",*pinstr-48);
        *argv[1]++;
        *pinstr++;;
        len++;
      }//if(*argv[1]-48 == 0 || *argv[1]-48 == 1)
      else {
        *argv[1] = '\0'; //to make an exit from the loop
        printf("\n");
        printf("**************************************************\n");
        printf("This program converts a binary number into integer\n");
        printf("Error: Incorrect Inputs\n");
        printf("Inputs can only be binary Digits i.e 0 or 1\n");
        printf("Example Usage:\n");
        printf("unix> ./a.out 10101010\n");
        printf("int_val = 170\n");
        printf("**************************************************\n");
        printf("\n");
        error = 1;
      }
    }//while(*argv[1] != '\0')
    *pinstr = '\0';
    pinstr = &instr[0];
    if(error == 0) {
      for(i=0;i<len;i++) {
        //printf("len = %d\n",len);
        poof2 = po(2,i) * (instr[len-i-1]-48);
        int_val = int_val + poof2;
      }
      printf("int_val = %d\n",int_val);
    }//if(error == 0)
////////////////////////////////////////////////////////////////////////////////
  }//if(argc == 2)
  else {
    error = 1;
    printf("\n");
    printf("**************************************************\n");
    printf("This program converts a binary number into integer\n");
    printf("Error: Incorrect Inputs\n");
    printf("Example Usage:\n");
    printf("unix> ./a.out 10101010\n");
    printf("int_val = 170\n");
    printf("**************************************************\n");
    printf("\n");
  } //else of if(argc == 2)
  //printf("error = %d\n",error);

} //main

//Functions Declaration Section
long int  po(short int x,short int y)
{
  //to Calculate 10^4, po(10,4);
  int j;
  long int ret=1;
  for(j=0;j<y;j++) {
    ret = ret*x;
  }
  return(ret);
}
//Function Declaration Ends
