1

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!

5
  • 2
    when converting from double to int your number can get truncated (use the same type -> use double) Commented Aug 17, 2011 at 8:00
  • 1
    You're converting from a binary string (eg "1010101"), right? I added "string" to the question title since that's a better description of what you want to do. Though actually converting from binary string to binary. Commented Aug 17, 2011 at 8:01
  • 1
    i am not converting from double to int all my data types are integer Commented Aug 17, 2011 at 8:02
  • 1
    @Octpus: that's a horrible answer, you just want to blindly fix the problem by observing the warning. you should read the code. Commented Aug 17, 2011 at 8:03
  • 2
    Also, don't use pow(2,..), use bitwise arithmetic, it's faster and cleaner. (the reason you're getting complaints about doubles is because pow() uses double as arguments and return value. Commented Aug 17, 2011 at 8:04

4 Answers 4

5

Why are you using pow to calculate a power of 2? It's too slow. Use 1 << p to get the p-th power of two. E.g., 1 << 0 will give 1, 1 << 1 will give 2, 1 << 2 will 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:

for(i=length;i>=0;i++)

If length is >= 0, the loop will never terminate.

This should fix it:

for(i = length - 1; i>=0; i--) sum += (binaryChar[i]*(1 << multiplier++));
Sign up to request clarification or add additional context in comments.

1 Comment

Actually the loop will terminate but only after the int type has wrapped around to negative. Before that the program is probably going to hit invalid memory access. :)
2

the signature of pow is:

double pow(double X, double Y);

for calculating 2^multiplier use:

1 << multiplier;

Just to quickly mention:

  • You have an infinite loop
  • If you parse the string from the other direction you can multiply the sum by two so you don't need the multiplier variable.
  • After that many problems I'm not even sure that int binaryChar[] is right. Char in the name suggests a different type (and code)..

.

1 Comment

Unfortunately that's only one of the many problems with the code.
1

pow takes doubles and returns a double. An ugly fix is just to use a cast

(int)pow(x, y)

But in this simple program, why not just do the power expansion yourself instead of calling pow?

Comments

1

The problem is that the precision of the data type int is smaller as the precision of double (the function pow returns double) therefore the value of binaryChar[i] will be implizit converted to double and so on... problematic line implicit looks like this:

sum = (double) sum + ((double) binaryChar[i] * pow((double)2, (double) multiplier))

in order to get rid of the warning you have to do an explicit conversion ( see Type Conversion), e.g.:

sum = sum + (binaryChar[i] * ((int) pow(2, multiplier));

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.