0

I have a code,

char* bin2hexchar( const unsigned char& ibin, char* pbcd )
{
    sprintf( pbcd, "%02X", ibin );

    return pbcd;
}

The problem is, the value of ibin variable will change to zero value.

Please advice.

Thanks

1
  • 4
    This is not C code, as ibin is a reference. Commented Sep 14, 2010 at 4:13

1 Answer 1

13

If your ibin is changing to a zero value in the caller to this function, the most likely explanation is buffer overflow.

I suspect it's probably because the buffer you're passing in as the second argument is defined thus:

char buff[2];

and ibin is adjacent to it on the stack.

The %02X format string requires three bytes, two for the characters and one for the terminating NUL character.

Even if that's not the specific case, it's still almost certainly buffer overflow. If so, please post the code that calls this function, along with the definitions for the relevant variables.

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

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.