-4

Sorry for my bad English first.

I have two binary files.

And I store binary into buffer respectively.

Then I compared two buffer using strcmp().

Result of strcmp() is zero.

So I think two binary is identical.

Open two binary and then checked if there are no differences.

But I can find little difference.

what is the problem?

strcmp() function doesn't proper way to compare binary to binary?

Jinwoo Bae
  • 35
  • 7
  • 1
    You are aware of the difference of binary and string. You do note that there ARE differences, though small. What is it that you do not understand? – Yunnosch Aug 31 '20 at 12:04
  • Why do you assume that two binary files are identical when you have tested them with a tool made for text and not binary? – klutt Aug 31 '20 at 12:08

2 Answers2

7

The C function strcmp is written to compare strings. In C, strings are char pointers or arrays, that end with a null byte ('\0'). Therefore, the comparison only goes up to the first null byte.

Example:

File A: "abcd\0efg" File B: "abcd\0xyz"

Since both files are equal up to the null byte, the "strings" at these locations are equal, although what comes after may differ. You should use the function memcmp instead (see this tutorial; see examples from the reference).

EDIT: As pointed out by the comment under this answer and as mentioned in the other answer, the man pages of strcmp and memcmp are reliable resources to learn about these function from the standard library.

Niklas Mohrin
  • 1,756
  • 7
  • 23
2

You cant compare binary data using string function.

You need to use memcmp instead.

https://man7.org/linux/man-pages/man3/memcmp.3.html

0___________
  • 60,014
  • 4
  • 34
  • 74