views:

29

answers:

2

I'm trying to write a bitmap (.bmp) parser/reader by reading raw bytes from the file and simply checking their values, and I've come across something I simply cannot wrap my mind around.

The image I'm trying to read is 512x512 pixels, and when I look at the width property (at 0x12 and 4 bytes onward) it says 00 02 00 00 (when viewed in a hex editor). I assume this is the same as the binary value 00000000 00000010 00000000 00000000. This somehow represents 512, I just cannot figure out the steps to get there.

So what I really need to know is how are integers represented binarily, and how do I parse them correctly? Any help is much appreciated. :)

A: 

Actually 0x200 in hex equals 512 in decimal. You may have the position of the width/height properties wrong.

Darin Dimitrov
+2  A: 

What you are seeing in your hex editor is actually right. Just remember that bytes are in little endian order, so the value is actually 00 00 02 00 = 0x0200 = 512.

3lectrologos
Ahh.. I just want to check that I understand this, let's say it said EB 88 00 00 instead, would that mean it should be interpreted as 00 00 88 EB = 0x88EB = 35051?
vrutberg
@vrutberg: Yes, that's right (of course that is valid only for a little-endian representation, which is used by almost all today's PCs). You needn't consider this, when you parse the values (it's only a matter of internal representation).
3lectrologos
Alright, thanks a lot!
vrutberg