Q:

I'm trying to convert binary to decimal

ok... the logic is..  I'm passing an integer variable having binary number... say (11010101) which is 213 in decimal....
the function will find the number of digits of the binary number which is 8.Then it will run a loop that many times..  then if kth digit of that binary number is 1 then it'll add 2^k to ans variable if kth digit of the binary number is 0... I'll just skip it..
so  1     =128 1     =64 0     =0 1     =16 0     =0 1     =4 0     =0 1     =1
      ------
       213
      ------
Thats the logic....but wheni run it... I keep getting the answer as
0... Can u guys help me out??
So here is the code..
int toDecimal(int number,int base){
    if (base==2) {
        int i, n=number, dec=0, d1, d=0;

        while(n!=0){
            n=n/10;
            ++d;
        }
        d1=d;

        for(i=0;i<d;i++){
            if(n%10==1){
                dec+=pow(2,--d1);
                n=n/10;
            }
            else{
                --d1;
                n=n/10;
            }
        }
        return dec;
    }
    return 0;
}

A:

for(i = 0; number != 0; i++){
    if(number % 10 == 1){
        dec += 1<<i;
    }
    number /= 10;
}

this should solve all problems discussed in the comments.

