分隔二进制序列 [英] Delimiting binary sequences

查看:35
本文介绍了分隔二进制序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够分隔二进制数据流.我正在考虑使用诸如 ASCII EOT(传输结束)字符之类的东西来执行此操作.

I need to be able to delimit a stream of binary data. I was thinking of using something like the ASCII EOT (End of Transmission) character to do this.

但是我有点担心——我怎么能确定用于此的特定二进制序列 (0b00000100) 不会出现在我自己的二进制序列中,从而在定界时给出误报?

However I'm a bit concerned -- how can I know for sure that the particular binary sequence used for this (0b00000100) won't appear in my own binary sequences, thus giving a false positive on delimitation?

换句话说,如何最好地处理二进制定界?

In other words, how is binary delimiting best handled?

...不使用长度标题.对不起,伙计们,之前应该提过这个的.

...Without using a length header. Sorry guys, should have mentioned this before.

推荐答案

你有五个选择:

  • 使用不太可能出现的分隔符.这冒着你猜错的风险.我不推荐这种方法.
  • 使用分隔符和转义序列来包含分隔符.您可能需要将转义字符加倍,具体取决于使解析更容易的原因.(想想 C 在某些内容中包含一个 ASCII NUL.)
  • 使用您可以确定不会出现的分隔符短语.(想想 mime 消息边界.)
  • 预先添加一个 length 字段,这样您就知道要读取以下 N 个字节作为数据.这样做的缺点是要求您在写入数据之前知道这个长度,这有时很困难或不可能.
  • 使用更复杂的东西,例如 ASN.1,来完整描述 所有您的内容.(我不知道我是否真的会推荐这个,除非你可以很好使用它——ASN.1 在最好的情况下使用起来很尴尬,但它确实允许完全明确的二进制数据解释.)
  • Use a delimiter character that is unlikely to occur. This runs the risk of you guessing incorrectly. I don't recommend this approach.
  • Use a delimiter character and an escape sequence to include the delimiter. You may need to double the escape character, depending upon what makes for easier parsing. (Think of the C to include an ASCII NUL in some content.)
  • Use a delimiter phrase that you can determine does not occur. (Think of the mime message boundaries.)
  • Prepend a length field of some sort, so you know to read the following N bytes as data. This has the downside of requiring you to know this length before writing the data, which is sometimes difficult or impossible.
  • Use something far more complicated, like ASN.1, to completely describe all your content for you. (I don't know if I'd actually recommend this unless you can make good use of it -- ASN.1 is awkward to use in the best of circumstances, but it does allow completely unambiguous binary data interpretation.)

这篇关于分隔二进制序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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