If I understand your question correctly, you want to pack the data in buf1 as a raw binary image. Then you want to unpack it.
The first problem is the usage of the std::vector's constructor. See the comment P1 in the following code:
#include <msgpack.hpp>
#include <iostream>
#include <iomanip>
#include <algorithm>
int main() {
int const num = 5;
msgpack::sbuffer buf1;
msgpack::packer<msgpack::sbuffer> bufPkr1(&buf1);
bufPkr1.pack_array(num);
for (int ndx = 0; ndx < num; ++ndx) {
bufPkr1.pack_array(3);
bufPkr1.pack(std::string("foo"));
bufPkr1.pack(std::string("bar"));
bufPkr1.pack(221);
}
// P1
// std::vector<char> chrVct = std::vector<char>(buf1.size(), *buf1.data());
std::vector<char> chrVct = std::vector<char>(buf1.data(), static_cast<char*>(buf1.data()) + buf1.size());
msgpack::unpacked unPkd1;
msgpack::unpack(&unPkd1, buf1.data(), buf1.size());
msgpack::object toStr1 = unPkd1.get();
std::cout << "MsgPack1: " << toStr1 << std::endl;
msgpack::sbuffer buf2;
msgpack::packer<msgpack::sbuffer> bufPkr2(&buf2);
// P2
// bufPkr1.pack_raw(chrVct.size());
// bufPkr1.pack_raw_body(chrVct.data(), chrVct.size());
bufPkr2.pack_raw(chrVct.size());
bufPkr2.pack_raw_body(chrVct.data(), chrVct.size());
msgpack::unpacked unPkd2;
msgpack::unpack(&unPkd2, buf2.data(), buf2.size());
msgpack::object toStr2 = unPkd2.get();
std::cout << "MsgPack2: " << toStr2.via.raw.size << std::endl;
std::for_each(toStr2.via.raw.ptr, toStr2.via.raw.ptr+toStr2.via.raw.size,
[](char e) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (static_cast<int>(e) & 0xff) << ' ';
});
std::cout << std::endl;
}
The std::vector's constructor in the original code is fill all the vector member to the same value that is passed as the second parameter.
That is the (2) of the following documentation:
http://www.cplusplus.com/reference/vector/vector/vector/
I believe that the third constructor, range version, should be used on this situation. So I replaced the original code with modified code.
The second problem is that bufPkr2 is never used. I guess that it is just typo. I commented out the original code and replaced it.
As the result of those modification, I got the following result:
MsgPack1: [["foo", "bar", 221], ["foo", "bar", 221], ["foo", "bar", 221], ["foo", "bar", 221], ["foo", "bar", 221]]
MsgPack2: 56
95 93 a3 66 6f 6f a3 62 61 72 cc dd 93 a3 66 6f 6f a3 62 61 72 cc dd 93 a3 66 6f 6f a3 62 61 72 cc dd 93 a3 66 6f 6f a3 62 61 72 cc dd 93 a3 66 6f 6f a3 62 61 72 cc dd