Char Buffer Comparison

I have two char buffers that I am trying to compare with them. I have a strange problem. I have the following code:

char buffer1[50], buffer2[60]; 
// Get buffer1 and buffer2 from the network by reading sockets
for(int i = 0; i < 20; i++)
{
    if(buffer1[15+i] != buffer2[25+i])
    {
        printf("%c", buffer1[15+i]);
        printf("%c", buffer2[25+i]);
        printf("%02x", (unsigned char)buffer1[15+i]);
        printf("%02x", (unsigned char)buffer2[25+i]);
        break;
    }
}

The code above is a simplified version of my actual code, which I have not copied here because it is too long. Just in case this can help, I got these two buffers over the network, reading sockets.

The problem is breaking the loop, even when both buffers are the same. To check what is in the buffers, I added two print statements inside the if statement. And oddly, printf statements print the same value for% c and% 02x, but the comparison fails and the loop breaks.

+3
source share
2 answers

(Disclaimer: I'm not an expert on C / ++)

, , . :

  • , ? , , .
  • ? , , for, .
+3

, , . if, , - .

+2

All Articles