0

I'm packing my string with this function:

std::vector<char> pack(const std::string& str) { const uint32_t sz = str.size(); const uint32_t n_sz = htonl(sz); std::vector<char> result(sizeof(sz) + sz); memcpy(result.data(), &n_sz, sizeof(n_sz)); memcpy(result.data() + sizeof(sz), str.data(), sz); return result; } 

How can i unpack it again so...i get the original string back?

I tried to do:

 int len; len = ntohl(ourbuffer.size()); char* string = ( char* )malloc(sizeof (char) * (len + 1)); string[len] = '\0'; 

becouse i know the function pack is using big endian. But that did not work. Can someone please show how to to unpack again??

1 Answer 1

1
uint32_t n_sz; memcpy(&n_sz, ourbuffer.data(), sizeof n_sz); const uint32_t sz = ntohl(n_sz); std::string str(ourbuffer.data() + sizeof n_sz, sz); 
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm that give a lot sense and works, thank you and to everyone else who helped me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.