Search code examples
pngjpegbmp

Why does opening an image file as text prints weird characters?


Everytime when I open jpeg, png, ico, etc, it always prints this. Why I opened it, well because I thought every software has a code but those software deals with image or colors happens to be something weird? So can anyone explain it?

MZ       ÿÿ  ¸       @                                   €   º ´    Í!  ¸      LÍ!This program cannot be run in DOS mode.

$       PE  L OhAY        à   8         þU       `    @                                  @…                           °U  K    `  ø                   €                                                                       H           .text   6       8                    `.rsrc   ø   `        :              @  @.reloc      €      >              @  B                àU       H     ¸+  ø)                                                       0      %{  
(  
*  0! 4    r  p{  
(  
Ð  r  pr  p  %r-  p¢%r1  p¢%r;  p¢%rE  p¢%rQ  p¢  %r-  pÐ  s  
¢%r1  pÐ  s  
¢%r;  pÐ  s  
¢%rE  pÐ  s  
¢%rQ  pÐ  s  
¢%r]  pÐ  s  
¢%re  pÐ  s  
¢r  p{  
(

Solution

  • Only plain text files are stored in, well, plain text. Images, programs, videos, music, and most other files are stored in various binary formats. When you open a binary file in a text editor, it assumes that the file you told it to open is plain text and starts reading the data in. Text editors read each chunk of data (which can be thought of as a series of numbers) in sequence and convert the data into the corresponding text character. Since the data in the file is binary, the data isn't intended to be displayed as characters and we see a tonne of random characters. That's a fairly big simplification, but it's close enough and should help you understand.

    As you can see there must be some plain text stored in the format as well since we can read This program cannot be run in DOS mode. and a few other random bits of text.

    Also, files on your computer are not programs unless they end in .exe (which is also a simplification, but close enough). Double-clicking an image file, for instance, tells the operating system to start up your image editing program and the OS tells the program to open the image. The image itself isn't a program.

    I would suggest that you read this, however: How do I ask a good question? This question is probably better-suited for https://superuser.com/.


    It's worth mentioning that, technically, every file is stored in binary, even plain text files. Plain text editors expect that each byte of the file corresponds to a single character (often from the ASCII table). When you open an image file in a plain text editor it will attempt to interpret each byte of the image file as text, but the bytes in the image file are not intended to be read as characters so they will instead come out as nonsense characters.

    It's like looking at the clock and replacing each number of the current time (say, 9:23) with a letter from the alphabet. The 9th letter of the alphabet is I, the 2nd is B and the 3rd is C, which gives us IBC. "But that's not a word!" you might say. Well of course not. We just tried to read the time as letters so it came out as nonsense. This is essentially what happens when you open an image file in a text editor.