Sorum basit.
Is there an equivalent of PHP's pack() and unpack() function in the C++ STL? If no, is there an alternative to achieve the same goal?
Teşekkürler.
STL hiçbir serileştirme mekanizması bulunmamaktadır. Eğer ne yapmak istediğinize bağlı olarak bir library such as the one in Boost kullanabilirsiniz ya da veri oldukça basittir eğer write your own serialization code, hangi özellikle bir alternatif olabilir olabilir.
Bir süre önce ben bu işlevi yapar C küçük bir kitaplığı yazdı. Burada Google kod benim kodunu yerleştirdiğiniz: code.google.com/p/packlib/
Yapılar çok PHP içinde açmak işlevi gibidir.
Bu kod parçaları, temel olarak eşdeğerdir.
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;
};