设计(二进制)文件格式时有哪些重点? [英] What are important points when designing a (binary) file format?

查看:36
本文介绍了设计(二进制)文件格式时有哪些重点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计记录二进制数据的文件格式时,您认为该格式应该具备哪些属性?到目前为止,我提出了以下要点:

When designing a file format for recording binary data, what attributes would you think the format should have? So far, I've come up with the following important points:

  • 在开头有一些魔术字节",以便能够识别文件(在我的具体情况下,这也应该有助于区分文件和旧"文件)
  • 开头有一个文件版本号,以便以后可以更改文件格式而不会破坏兼容性
  • 指定所有数据项的字节顺序和大小;或:包括一些空间来描述数据的字节顺序/大小(我倾向于前者)
  • 可能为将来可能需要的其他每个文件属性保留一些空间?

还有什么有用的方法可以使格式更加面向未来并尽量减少将来的麻烦?

What else would be useful to make the format more future-proof and minimize headache in the future?

推荐答案

看看 PNG 规范.这种格式背后有一些很好的理由.

Take a look at the PNG spec. This format has some very good rationale behind it.

另外,确定对您未来的格式最重要的是:紧凑性、兼容性、允许在其中嵌入其他格式(不同的压缩算法).另一个有趣的例子是 Google 的协议缓冲区,其中传输数据的大小为王.

Also, decide what's important for your future format: compactness, compatibility, allowing to embed other formats (different compression algorithms) inside it. Another interesting example would be the Google's protocol buffers, where size of the transferred data is the king.

至于字节顺序,我建议您选择一个选项并坚持使用,不允许不同的字节顺序.否则,读写库只会变得更复杂、更慢.

As for endianness, I'd suggest you to pick one option and stick with it, not allowing different byte orders. Otherwise, reading and writing libraries will only get more complex and slower.

这篇关于设计(二进制)文件格式时有哪些重点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆