C ++ equivalent PHP package ()

My question is simple.

Is there an equivalent to the PHP pack () and unpack () function in C ++ STL? If not, is there an alternative to achieve the same goal?

http://us.php.net/pack

Thank.

+3
source share
4 answers

If your goal is to serialize data, you can use the Google protocol buffers to achieve it.

http://code.google.com/apis/protocolbuffers/

+3
source

STL . , , , Boost, , , .

+2

A back, I wrote a small library in C that performs this function. I put my code in google code here: code.google.com/p/packlib/

+1
source

Structures are very similar to the decompress function inside PHP.

These pieces of code are basically equivalent.

PHP:

define('ISP_TINY', 4);
class IS_TINY
{
    const PACK = 'CCCC';
    const UNPACK = 'CSize/CType/CReqI/CSubT';

    public $Size = 4;
    public $Type = ISP_TINY;
    public $ReqI;
    public $SubT;

    public function __construct($rawPacket)
    {
        $pkClass = unpack($this::UNPACK, $rawPacket);
        foreach ($this as $property => $value)
        {
            $this->$property = $pkClass[$property];
        }
    }
}

C ++:

#define ISP_TINY = 4;
struct IS_TINY // General purpose 4 byte packet
{
    byte Size; // Always 4
    byte Type; // Always ISP_TINY
    byte ReqI;
    byte SubT;
};
0
source

Source: https://habr.com/ru/post/1738869/


All Articles