ELI5: Why does opening an executable in Notepad and saving it again as an executable fail to work?

Clarifying the parent answer a bit, because it is a bit wrong! Notepad does not use ASCII characters but usually ANSI, which refers to system defaults, for example windows often uses Windows-1252 encoding.

Also when notepad reads the data, it indeed preservers (hint: most of) the original bytes. Therefore reading a file with some encoding and then saving it with same encoding should not break it. Whats the problem then?

Here we can see the raw data contents of windows help program hh.exe, as viewed with HxD hex editor. On the left is the original and on the right is the version that went trough notepad.exe. What we can clearly see is that notepad does not like the null; a byte with value 00, because it does not represent anything. So after notepad has been used, all those nulls are changed to 20 which is ASCII value for spacebar. More sophisticated text editors, for example notepad++ can handle nulls as well.

So any byte sequence can be interpreted as a character, and then writing back that character with same encoding does not change the file. File contents and their representation are always separate.

If user would change all those 20's back to 00, the program would work again (careful though, because the original also contains some bytes with value 20, so changing them to 00 would also break it.

/r/explainlikeimfive Thread Parent