tags:

views:

126

answers:

2

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

+2  A: 

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;
Andreas Brinck
This is one way though but is it really safe since reinterpret_cast is considered pretty dangerous to use
TP
A: 

More details on the message layout or format would make answering a lot easier. Also, an example of the input data would be appreciated.

The fastest method to parse is probably to use `sscanf`; however it is not the safest method. Using extract from an `istream` is safer.

If input is your bottleneck, try hauling in the data into a large buffer (`unsigned char`) or use `std::string`. If using `std::string`, reserve a maximum amount before using the string, which will cut down on some of the overhead.

Thomas Matthews