83
22
This phenomenon has been leaving me questions to ask.
Here is the detailed experiment, my OS is Windows 7 x64 SP1:
- I changed a picture (JPG) file to TXT by simply changing its extension (or one could just choose to open the JPG with notepad, same thing)
It should look like this, oddly looking sequences of texts, and some of them (very rare) are actually meaningful, like in the screenshot below "creator: dg-jpeg v1.0..."
- I disabled wrapping and selected all the text using Ctrl+A (to make sure nothing's missed)
- I pasted the copied text to another blank TXT file and saved it as JPG, I compared the new file size with the original JPG. All of them (the original JPG, the converted TXT file and the newly created TXT file) are of the exact same size, to bytes.
When I tried to open, Windows would say "Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted, or is too large".
I even tried to test it using another method: Opened the JPG with notepad, I cut ONE known character from a location easy to remember (like the first character of the 2nd line) then save the file. The viewer would of course display the same message. Then I opened it again and pasted the character to the EXACT location (Notepad remembers its exit state like windows position, wrapping, fonts size...so I have no problem getting this right)
And still the same error. You can try this to get the idea, remember to choose a small picture else Notepad will act like a old rusty man.
What could have been the cause of this phenomenon?
4Try the fc command. open a cmd prompt and do-
C:\blah>fc file1 file2
It is possible for files to be the same size but different. (though usually some random change doesn't tend to leave a file the same size but it easily could). The fc command will be very useful to you in investigating what is happening. You can also use the xxd command, this is in cygwin, and also comes with vim7. xxd -p file1 That will dump the hex of a file. You can compare the hex of the two files with that and fc. Or even open the hex in notepad and flick between the two notepad windows with alt-tab. – barlop – 2014-07-13T21:18:01.37723You are trying to read a binary file with a simple text editor like notepad. It won't be able to read the ANSI encoding correctly and thus it will convert it. When you save it then the file won't be binary anymore and thus the parser can't read the data inside the file. (Lookup the difference between XML based file saving and Binary file saving it's an interesting topic.) If you would try the same experiment with Notepad++ you'll succeed in what you were trying. – woutervs – 2014-07-14T10:32:55.433
1
possible duplicate of Why does an exe file not appear as ones and zeros in a text editor such as Notepad?
– allquixotic – 2014-07-14T15:07:53.9373
For the interested: You can edit images in Vim: However, the trick is, that Vim converts the file in the XPM format, which is plain ASCII.
– Boldewyn – 2014-07-14T15:35:37.2301
@ÃŁŁǫǛȉЖΦΤїҪ (@allquixotic) I disagree; this question is not a dup of Why does an exe file not appear as ones and zeros …?; that question does not address the fact that Notepad makes changes to binary files that are not requested by the user (which is what this question is all about). OTOH, Save Raw Image Data As Image is somewhat close – but still not similar enough for me to VTC.
– Scott – 2014-07-14T17:30:48.173@Scott Well it does say possible duplicate... and that question was mostly for reference anyway; I should've posted a comment saying "See Also: <link>" but I was too lazy :S – allquixotic – 2014-07-14T17:39:36.927
Try using a hex editor. – Panzercrisis – 2014-07-14T20:34:36.320
4Long story short, Notepad modifies your file before displaying it to you. – Derek 朕會功夫 – 2014-07-15T23:21:43.723
Any idea that it'll work in Notepad++? – Nguyễn Tuấn Danh – 2014-07-18T10:44:29.580
@NguyễnTuấnDanh Why even ask about Notepad++? Don't use a text editor to edit arbitrary data, it's entirely the wrong tool for the job. Use a proper hex editor or, even better, something designed to edit JPEG images (like e.g. an image editor). I'm not sure what you were expecting to happen by opening random strings of bytes with a program intended for text editing. It's like trying to edit an MP3 with Photoshop. – Jason C – 2014-07-19T15:57:29.737
I.e. You know that game that's sometimes fun to play where you type some text into a translator, translate it to a language then back to yours, and laugh at the poor grammar in the results? That's essentially what you've done by loading and saving JPEG data in a text editor. – Jason C – 2014-07-19T16:01:49.190
@woutervs "If you would try the same experiment with Notepad++ you'll succeed". tried with notepad++ and sublime Text failed. – Saif – 2015-12-07T05:12:18.033
@Saif For the sake of the experiment I've also tried it, worked perfectly on my end with default N++ settings. – woutervs – 2015-12-07T08:29:03.160