pack in c++

L

Larry

Hi,

is there any pack function in C++ like that one in Perl?

I need to serialize a Key and a Value in one single string, something like:

KEY1....VALUE

Where KEY1 is 4 bytes long (fixed long)
..... is an unsigned long (32-bit) rappresenting the lenght of VALUE
VALUE is the rwa data

the following is how I'd go about doing it in Perl:

my $key = 'KEY1';
my $value = "Hello World!";
my $value_len = length $value;
my $res = $key . pack("N", $value_len) . $value;

can it actually be done in C++ the same way?

thanks
 
G

Gert-Jan de Vos

Hi,

  is there any pack function in C++ like that one in Perl?

I need to serialize a Key and a Value in one single string, something like:

KEY1....VALUE

Where KEY1 is 4 bytes long (fixed long)
.... is an unsigned long (32-bit) rappresenting the lenght of VALUE
VALUE is the rwa data

the following is how I'd go about doing it in Perl:

my $key       = 'KEY1';
my $value     = "Hello World!";
my $value_len = length $value;
my $res       = $key . pack("N", $value_len) . $value;

can it actually be done in C++ the same way?

thanks

Like this maybe:

#include <iostream>
#include <string>

class pack_N
{
public:
pack_N(int value) : m_value(value) {}

friend std::eek:stream& operator<<(std::eek:stream& os, const pack_N& pack)
{
return os.put((pack.m_value >> 24) & 0xFF).put((pack.m_value >> 16)
& 0xFF).put((pack.m_value >> 8) & 0xFF).put(pack.m_value & 0xFF);
}

private:
unsigned m_value;
};

void serialize(const std::string& key, const std::string& value)
{
std::cout << key << pack_N(value.size()) << value;
}

int main()
{
serialize("KEY1", "Hello World!");
}
 
L

Larry

thanks!!

since I'd like to append the serialize() return value to a main var, do you
think I could modify your code something like this:

std::string serialize(const std::string& key, const std::string& value)
{
return key + pack_N(value.size()) + value;
}

int main()
{
std::string container = "";

container += serialize("KEY1", "Hello World!");
container += serialize("KEY2", "How are you?");
//...etc...
}

Did I get anything wrong??

thanks
 
G

Gert-Jan de Vos

thanks!!

since I'd like to append the serialize() return value to a main var, do you
think I could modify your code something like this:

That's what stringstream is for:

void serialize(std::eek:stream& os, const std::string& key, const
std::string& value)
{
    os << key << pack_N(value.size()) << value;
}

int main()
{
    std::stringstream container_stream;

serialize(container_stream, "KEY1", "Hello World!");
    serialize(container_stream, "KEY2", "How are you?");
    //...etc...

std::string container = container_stream.str();
}

You can now make overloads for the serialize() function for
serialization of different datatypes. If you use a file stream
for serialization, remember to open it in binary since you are
writing binary data to it.
 
L

Larry

"Gert-Jan de Vos" <[email protected]> ha scritto nel
messaggio
void serialize(std::eek:stream& os, const std::string& key, const
std::string& value)
{
os << key << pack_N(value.size()) << value;
}

Just one more question!

is:

os << key << pack_N(value.size()) << value;

the same to:

os + key + pack_N(value.size()) + value;

???

thanks
 
G

Gert-Jan de Vos

Just one more question!

is:

os << key << pack_N(value.size()) << value;

the same to:

os + key + pack_N(value.size()) + value;

No, the first calls operator<<(std::eek:stream&, const std::string&)
The second tries to call operator+(std::eek:stream&, const std::string&)
which doesn't exist as far as I know. operator<<() is the idiomatic
C++ way of serializing objects into a stream. This is how the
standard library does it. Your C++ language tutorial should explain
this in detail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top