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.
x = pow(2, count);, asxis anint, andpow()returnsdouble. The integer equivalent ofpow(2, count)is1 << count.x = 1 << countinstead ofx = pow(2,count).xasunsigned. Otherwise, it will overflow when you try to set it topow(2, 31).