开发者

Using ruby to find frames in a binary protocol and store each frame in an array

I have a binary file, inside of which has multiple frames. Each frame starts with FF FF FF followed by random data of varying length. For example:

FF FF FF XX XX XX XX ......... FF FF FF XX XX XX XX XX XX ......... FF FF FF XX XX XX .... FF FF FF XX XX XX XX XX XX XX XX XX ........

It's safe to assume that FF FF FF will only appear in the frame header and not as part of the random data.

开发者_JS百科I'm looking for a way of parsing this binary file and extracting each frame into an array using ruby.

Can anyone help?


For Ruby 1.8, you can just split the input string on "\xFF\xFF\xFF". The first entry will be before the first frame, and with such a format you cannot know whether the last frame is complete or not. They are simple enough to remove, though:

input.split("\xFF\xFF\xFF")[1..-2]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜