Salve,

volevo sapere se in C++ esiste una funzione simile a Pack in Perl.

Nello specifico mi serviva per creare stringhe binarie composte da:

Key (lunghezza fissa 4 Bytes) + Lunghezza Value (lunghezza fissa 4 Bytes) + Value

Quindi ad esempio:

KEY1....VALUE

Dove KEY1 è la chiave (4 Bytes) poi .... sono quello che quì intendo come non printable chars (4 Bytes) che descrivono la lunghezza di VALUE, infine VALUE.

Cosi che possa creare una stringa binaria tipo:

KEY1....VALUEKEY2....VALUE etc...

in Perl avrei fatto qualcosa come:

codice:
my $key       = 'KEY1';
my $value     = "Hello World!";
my $value_len = length $value;
my $res       = $key . pack("N", $value_len) . $value;
Dove la N di Pack sta per:

"N" - an unsigned long (32-bit) in "network" (big-endian) order.
che occupa 4 bytes.

Naturalmente in seguito mi piacerebbe "portare" altri elementi tipo:

"n" - an unsigned short (16-bit) in "network" (big-endian) order.
"a" - a string with arbitrary binary data, will be null padded.
"A" - a text (ASCII) string, will be space padded.
"c" - a signed char (8-bit) value.
Grazie