开发者

how can i represent a packet (binary stream) in a variable (c++)

开发者 https://www.devze.com 2022-12-19 01:15 出处:网络
hi i\'m working on a personal project for a transport parser. i want to be able to represent a recived packet in binary number and afterwards be able to set specific bits.

hi i'm working on a personal project for a transport parser.

i want to be able to represent a recived packet in binary number and afterwards be able to set specific bits. I've got a pr开发者_运维问答etty good idea how to do the second part but i'm really stuck at the beginning ive got an advice to use unsigned char for that but can i really represent a full packet in that variable.

thanks


an unsigned char array is probably what you need: you can store whatever you want in this structure and access it in whatever means pleases you.

You could have this container in a bigger container too: the bigger container would have pointers to the each layer's beginning & end etc.


I'd probably have a simple class (simple to begin with anyway):

class Packet
{
public:
 Packet(unsigned int length);
 Packet(void *data);
 bool getBit(unsigned int bit);
 void setBit(unsigned int bit,bool set);
private:
 std::vector<unsigned char> bytes;
};

That's just to start, no doubt it would get more complex depending what you use it for. You might consider overloading the array operator but that's probably outside "beginner level" and maybe best ignored right now.

0

精彩评论

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

关注公众号