Delimiting binary sequences

后端 未结 6 2049
甜味超标
甜味超标 2020-12-30 02:21

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.

However I\'m a

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 02:58

    You've got five options:

    • 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 \0 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.)

提交回复
热议问题