1

Here is a snippet of the first part of a PNG file viewed just by opening it up in Sublime Text and viewing the hex code directly.

8950 4e47 0d0a 1a0a 0000 000d 4948 4452
0000 02e7 0000 0155 0806 0000 004c ed41
2b00 0000 0662 4b47 4400 ff00 ff00 ffa0
bda7 9300 0000 0774 494d 4507 d907 1209
3738 0601 8766 0000 2000 4944 4154 789c
ecbd 799c 6567 5de7 fff9 3ef7 5675 279d
a4b3 d008 1242 2288 c486 10e8 ee74 57a7
bb53 28a2 202e a8c1 0d07 d780 a8a3 8338
fc04 c601 45dc 1750 5161 1670 e437 0e71
5c90 4504 a549 77ba 7a65 0f20 8681 8140
423a 2b49 7aa9 aafb 7ce7 8fb3 dce7 3ce7

It seems like you could save some space by removing the whitespace in the file. I'd be interested to know if that would be an optimization technique for PNGs. Or perhaps there is some advantage or purpose to having these 4-character sequences separated by 1 space that I am not aware of.

4
  • 3
    What do you mean, whitespace? Those columns between, e.g. 8950 and 4e47 aren't actually there in the file. Commented Aug 21, 2018 at 5:15
  • I am so confused. In Sublime Text I can highlight a space in the columns, so I don't understand.
    – user39251
    Commented Aug 21, 2018 at 5:27
  • Is it actually a valid PNG file (can you open it in an image viewer) and not the output of, say, XXD? XXD can group bytes by two, but I don't know if you can make it omit the plaintext column. Commented Aug 21, 2018 at 5:32
  • Yes this is just a regular png. I get the same results when viewing a screenshot taken with the Mac.
    – user39251
    Commented Aug 21, 2018 at 5:35

1 Answer 1

2

The whitespace isn't in the file. What you're seeing isn't text – it's a list of hexadecimal numbers, and a number is the same whether it has spaces in the middle or not. So it's still four bytes whether you write them as 89 50 4e 47 or 89504e47 or as (137, 80, 78, 71) or indeed as �PNG.

You're probably seeing this because Sublime Text detects that the file's contents can't be sensibly shown as text. So it chooses an alternative representation – each byte as two hex digits. (For a better example, take a look at this plugin for sublimetext.)

Recall that every file is made out of bytes, and a byte is a number between 0 and 255 decimal (or 0 to FF hexadecimal). Although text editors generally show each byte as a letter, that's not the only way to do it.

3
  • Thank you, I am finally starting to understand. I don't get why it's so hard to just open the binary file and see 001010011 a bunch of 1's and 0's, that would be nice.
    – user39251
    Commented Aug 21, 2018 at 6:12
  • @LancePollard: It's hard because you're trying to do it with a text editor, which isn't built with that in mind. (Even by having hex mode at all, Sublime is already beyond most text editors.) Commented Aug 21, 2018 at 6:18
  • 2
    @LancePollard: But it's also a useless thing to ask for – even if it were binary, you'd be seeing the same numbers, just in a format that's much more verbose, much harder to work with. Again, it does not matter whether you see the numbers in hexadecimal, in binary, in decimal, in octal, these are the same numbers. Hex editors use hex because it's the easiest to work with. Most people who need this feature, if they saw binary, would say: "I don't get why it's so hard to just open the file and hexadecimal 89504e..., that would be nice." Commented Aug 21, 2018 at 6:19

You must log in to answer this question.