0

So I have an algorithm designed to take in a 32 bit char array and translate it into its proper decimal equivalent.

double calculateDecimal(char *string){
    char *temp=string;
    double total=0;
    int i;
    int count=31;
    int x=pow(2,count);
    for(i=0;i<32;i++){
        if(!strncmp(temp,"1",1){
            printf("%d + %d\n",x,total);
            total+=x;
        }
        temp++;
        count--;
        x=pow(2,count);
    }
    printf("%d\n",total);
    return total;

}

1) My printf statements have proved that the proper 1's are being read and their proper powers are being calculated. What I have discovered is total and x keep equaling the same power which I'm confused about because I'm on the cusp.

2)My example calculation was 00000000100000010000000000000111 which if i typed in correctly should be the decimal equivalent of 84554151. Thanks for any contributions because I know I am close.

8
  • 1
    What is a "32 bit char array"? Commented Sep 21, 2013 at 0:09
  • The problem is most certainly x = pow(2, count);, as x is an int, and pow() returns double. The integer equivalent of pow(2, count) is 1 << count. Commented Sep 21, 2013 at 0:10
  • And expanding on that, you probably want to just do x = 1 << count instead of x = pow(2,count). Commented Sep 21, 2013 at 0:11
  • I defined an array called char finalBinary[33] to make room for the null terminator. When I changed x to be a pow it opened up a whole new can of words with the proper pow's not being calculated. Commented Sep 21, 2013 at 0:13
  • 3
    You should declare x as unsigned. Otherwise, it will overflow when you try to set it to pow(2, 31). Commented Sep 21, 2013 at 0:15

2 Answers 2

2

If it's a 32-bit input, why not use a 32-bit integer to contain the result? The pow function is a floating point operation, which makes the task harder. Consider bit operations instead.

int toInt(char *str)
{
  int val = 0;
  while (*str) 
    val = (val << 1) | (*str++ == '1');
  return val;
}

Also note that by shifting the previous result left (multiplying by 2) each time a new character is found, this will work with any string up to 32 bits long.

Sign up to request clarification or add additional context in comments.

3 Comments

I think your answer would be more helpful to the beginner if you split the calculation and increment up into separate statements, instead of trying to do it as tersely as C allows.
@Barmar I think not. Check again.
@Barmar He's doing fine. Looking up the idioms in K&R will be straightforward for him.
1

Well, your code actually has a syntax error here (missing closing bracket):

if(!strncmp(temp,"1",1){

Regarding your problem of binary --> decimal conversion, I would recommend Horner's method of evaluating polynomials: http://en.wikipedia.org/wiki/Horner%27s_method

View the binary expansion as a polynomial where each coefficient is either 0 or 1. Evaluating this polynomial for x = 2 gives you the actual value, which you can print in decimal:

long calculateValue(char * string) {
    long result = 0;
    while(*string){
        result = ((*string) - '0') + result * 2;
        string++;
    }
    return result;
}

(And please don't use pow() and other floating-point functions for integer operations - especially for calculating powers of 2)

BTW, you can use this approach for evaluating numbers written in any base:

long calculateValue(char * string, int base) {
    long result = 0;
    while(*string){
        result = ((*string) - '0') + result * base;
        string++;
    }
    return result;
}

Of course this works for base 1-10, because '9' is followed by ':' in the ASCII table, but you get the idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.