开发者

What is the fastest way to deconstruct a fixed length binary/alpha message?

What would you suggest as the fastest or best way to parse a fixed length message in c++ which has 开发者_运维问答fields defined like

field = 'type', length = 2, type = 'alphanumeric'

field = 'length', length = 2, type = 'binary' (edit:length = 2 means 16 bit)
...
...

and so on

I read about making a struct and then using reinterpret_cast but im not sure how to use that or if there is any better method.

By parsing, i mean extracting human readable format like 'Type = X', 'Length = 15' etc


Is this what you mean?

char* binaryMessage; //From somewhere
struct Fields {
    short type; // 2 bytes
    short length; // 2 bytes
};
Fields* fields = reinterpret_cast<Fields*>(binaryMessage);
std::cout << "Type = " << fields->type;
std::cout << "Length = " << fields->length;

A safer alternative is boost::basic_bufferstream:

basic_bufferstream<char> stream(binaryMessage, lengthOfMessage, std::ios_base::in);
Fields fields;
stream >> fields.type;
stream >> fields.length;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜