读取并解析位图的宽度/高度属性
我试图通过从文件中读取原始字节并简单地检查它们的值来编写位图(.bmp)解析器/读取器,但我遇到了一些我根本无法理解的事情。
我尝试读取的图像是 512x512 像素,当我查看 width 属性(在 0x12
和 4 个字节以上)时,它显示 00 02 00 00
(当在十六进制编辑器中查看)。我假设这与二进制值00000000 00000010 00000000 00000000
相同。这个不知何故代表512,我只是不知道到达那里的步骤。
所以我真正需要知道的是整数如何以二进制表示,以及如何正确解析它们?非常感谢任何帮助。 :)
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. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在十六进制编辑器中看到的实际上是正确的。请记住,字节采用 little endian 顺序,因此该值实际上是
00 00 02 00 = 0x0200 = 512。
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
.实际上,十六进制的
0x200
等于十进制的512
。您的宽度/高度属性的位置可能错误。Actually
0x200
in hex equals512
in decimal. You may have the position of the width/height properties wrong.