0

İ started to understanding how binary files works. İ am trying to make a program to get png's width and height. İ followed what wikipedia say's but that didn't work for me. Then i opened a hex editor and relised that the 4 bytes (int) flipped so when i flip them again i get the true value

59 50 4e 47 0d 0a 1a 0a // <- PNG header
00 00 00 0d 49 48 44 52 // 4 bytes for chunk length (13 byte) and 4 byte for chunk name (İHDR)
00 00 03 20 00 00 02 58 //4 bytes for width(800px) and 4 bytes for height(600px)
...

This is what hex editor showing. Like for chunk length it must be 0d 00 00 00 but above its 00 00 00 0d or width its must be 20 03 00 00

İs this normal in binary files? That you need to flip the bytes order to get the true value of int?

This is not just in hex editor. İ flipped the bytes in c++ code to get the value of the integers!

Star Dev
  • 59
  • 4
  • 1
    There is no rule of law that specifies byte order in every binary file. This depends entirely on the file format. Somewhere in the png file specification it is specified what the byte order is. You'll have to find where it is and read what it says. Or, use any one of the many freely-available png-reading libraries, and leave it up to them to figure everything out. – Sam Varshavchik Jul 16 '22 at 00:37
  • In my opinion, it is not normal in binary files. Many formats (for example, BMP and ZIP) uses little-endian. On the contrary, [PNG uses big-endian](https://www.w3.org/TR/2003/REC-PNG-20031110/#7Integers-and-byte-order). – MikeCAT Jul 16 '22 at 00:37
  • 1
    Uh the terms you want to search for an read up on is "Endianness" will change the data order, also whether your machine is 32 bit or 64 bit will change the length of each entry. Assuming you are correct that it will all be stored in the way you expect. – Natio2 Jul 16 '22 at 00:39

0 Answers0