-
-
Save ztane/6ac76696eca859563d99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ripped from http://stackoverflow.com/questions/4749325/parsing-binary-message-stream-in-c-c/4749570#4749570 | |
// a deleted SO answer by http://stackoverflow.com/users/166749/larsmans | |
class unpack | |
{ | |
uint8_t *bufo, *bufp; | |
public: | |
unpack(uint8_t *buf) : bufo(buf), bufp(buf) { } | |
unpack &operator>>(uint8_t &x) | |
{ | |
x = *bufp++; | |
return *this; | |
} | |
unpack &operator>>(uint16_t &x) | |
{ | |
x = *bufp++ << 8; | |
x |= *bufp++; | |
return *this; | |
} | |
unpack &operator>>(uint32_t &x) | |
{ | |
x = *bufp++ << 24; | |
x |= *bufp++ << 16; | |
x |= *bufp++ << 8; | |
x |= *bufp++; | |
return *this; | |
} | |
// etc. for uint64_t; roll your own for floats | |
}; | |
// Usage: read data from the wire into uint8_t buffer[SIZE_OF_MESSAGE], then | |
struct MsgData msg; | |
unpack(buffer) >> msg.num >> msg.x >> ... >> msg.elevation; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment