In the following code I am converting a binary to decimal and then printing the character corresponding to it.
void convertToChar(int binaryChar[],int length)
{
int multiplier = 0;
int i;
int sum = 0;
for(i=length;i>=0;i++)
{
sum = sum + (binaryChar[i]*pow(2,multiplier));
multiplier = multiplier + 1;
}
printf("\nThe character is: %c",sum);
}
The problem is in the line sum = sum + (binaryChar[i]*pow(2,multiplier)); .It throws the error: warning: converting toint’ from double'.Please help!
Why are you using
powto calculate a power of 2? It’s too slow. Use1 << pto get the p-th power of two. E.g.,1 << 0will give 1,1 << 1will give 2,1 << 2will give 4. This is due to the nature of the bit shift operation: shifting one bit to the left is equivalent of multiplying by 2.Also, it looks like you have an endless cycle in your program:
If
lengthis >= 0, the loop will never terminate.This should fix it: