I was reading BMP file in hex editor while discovered something odd. Two first letters "BM" are written in order, however the next word(2B), which is means file size, is 36 30 in hex. Actual size is 0x3036. I've noticed that other numbers are stored the same way. I'm also using MARS MIPS emulator which can display memory by words. String in.bmp is stored as b . n i / \0 p m.

Why data isn't stored continuously?

有帮助吗?

解决方案

It depends not on the data itself but on how you store this data: per byte, per word (2 bytes, usually), or per long (4 bytes -- again, usually). As long as you store data per byte you don't see anything unusual; data appears "continuous". However, with longer units, you are subject to endianness.

It appears your emulator is assuming all words need to have their bytes reversed; and you can see in your example that this assumption is not always valid.

As for the BM "magic" signature: it's not meant to be read as a word value "BM", but rather as "first, a single byte B, then a single byte M". All next values are written in little-endian order, not only 'exchanging' your 36 and 30 but also the 2 zeroes 'before' (or 'after') (the larger values in the BMP header are of 4 bytes long type).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top