-1

I am trying to convert a struct into msgpack in C or C++. Please find the code below:

struct your_type {
    int a;
    float b;
    MSGPACK_DEFINE(a, b);
};

int main() {
    // packing
    std::stringstream ss;
    std::vector<std::map<std::string, your_type>> v 
    { 
        { 
            { "t", {1,2.7} }

        },
        {
            {"value", {2, 6.8} } 
        }
    };
    msgpack::pack(ss, v);
}

I understand how to transform this output to object using msgpack::object_handle. But I want to print the packed output in C, so to verify if it does look like the format we obtain after converting a dataframe into msgpack in Python. How can I display the msgpack::pack output?

Edited: hex_dump() as mentioned answer provided in the comments actually helps in obtaining the answer.

struct your_type {
    int a;
    int b;
    MSGPACK_DEFINE(a, b);
};

int main() {
    std::stringstream ss;

    std::vector<std::map<std::string, your_type>> v
    {
        {
            { "t",{ 1, 2 } }

        },
        {
            { "value",{ 3, 6 } }
        }
    };
    msgpack::pack(ss, v);
    hex_dump(std::cout, ss.str()) << std::endl;
}

I am able to convert this struct into hexvalues. But I am trying to make a float value for b in struct, so that "value" can take float values. I am not able to do so though. Could anyone please guide me in this?

  • 1
    This is C++, not C... – Mad Physicist Nov 07 '18 at 11:45
  • What is a "message packed structure"? – Andrew Henle Nov 07 '18 at 11:47
  • 1
    I am unclear as to why this question has the dataframe and python tags. – Mad Physicist Nov 07 '18 at 11:48
  • @Andrew: https://en.m.wikipedia.org/wiki/MessagePack. That tag actually makes sense. – Mad Physicist Nov 07 '18 at 11:49
  • What are you actually asking? How to print the contents of `ss`? Are you trying to create a file? Or dump to the console? – Mad Physicist Nov 07 '18 at 11:51
  • Sorry if my question was not proper. I did not want to put the whole question here. I know how to convert a pandas dataframe to msgpack in Python. The output looks like this : b'\x82\xA1\x74\x92\x01\xCB\x40\x05\x99\x99\x99\x99\x99\x9A\xA5\x76\x61\x6C\x75\x65\x92\x02\xCB\x40\x1B\x33\x33\x33\x33\x33\x33'. I want to convert similarly a struct to such a format C or C++, which later could be passed to Python. Could anyone please guide me? – Aparna Bose Nov 07 '18 at 12:05
  • How about hex_dump() function in this tutorial https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_tutorial#single-value ? – Takatoshi Kondo Nov 08 '18 at 00:13
  • @TakatoshiKondo Thank you very much for your reply. It got me close to the answer I was seeking for. Shall I ask you a doubt please – Aparna Bose Nov 08 '18 at 09:44
  • @TakatoshiKondo Could you please help me in including a float value inside struct instead of int values. I have tried it but it throws me error. Please see the updated question for code. – Aparna Bose Nov 08 '18 at 09:52
  • I post my answer that contains running code based on your updated question. – Takatoshi Kondo Nov 08 '18 at 10:46

2 Answers2

0

msgpack::pack writes the packed content into its first parameter of a type std::stringstream, so it should be easy to print it:

msgpack::pack(ss, v)
std::string string_buffer = ss.str()
std::cout << s << '\n';
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
  • Thank you for your response. I have tried str() and rdbuf() but I am not able to understand the output – Aparna Bose Nov 07 '18 at 12:01
  • Sorry if my question wasn't proper, I would want an output that looks like b'\x82\xA1\x74\x92\x01\xCB\x40\x05\x99\x99\x99\x99\x99\x9A\xA5\x76\x61\x6C\x75\x65\x92\x02\xCB\x40\x1B\x33\x33\x33\x33\x33\x33' In Python, as we convert a dataframe to msgpack, the output looks like this. Shall I also get like the same in C or C++ – Aparna Bose Nov 07 '18 at 12:01
  • The output with str() is not readable as in Python. Could you please help me? – Aparna Bose Nov 07 '18 at 12:11
0

After some conversation, I understand your question contains two different part. One is how to print packed data as human readable hex format similar to python. The other is how to treat well float value in msgpack.

Here is the code to answer both two parts.

#include <sstream>
#include <iostream>
#include <msgpack.hpp>

inline std::ostream& hex_dump(std::ostream& o, std::string const& v) {
    std::ios::fmtflags f(o.flags());
    o << std::hex;
    for (auto c : v) {
        o << "\\x" << std::setw(2) << std::setfill('0') << (static_cast<int>(c) & 0xff);
    }
    o.flags(f);
    return o;
}

struct your_type {
    int a;
    float b;
    MSGPACK_DEFINE(a, b);
};

int main() {
    // packing
    std::stringstream ss;
    std::vector<std::map<std::string, your_type>> v 
    { 
        { 
            { "t", {1,2.7} }

        },
        {
            {"value", {2, 6.8} } 
        }
    };
    msgpack::pack(ss, v);

    // hex dump output
    hex_dump(std::cout, ss.str()) << std::endl;

    auto oh = msgpack::unpack(ss.str().data(), ss.str().size());
    // JSON output
    std::cout << oh.get() << std::endl;
}

Runnning Demo: https://wandbox.org/permlink/DanDq9QTW6JZ6MO0

The funtion hex_dump() outputs std::string to ostream as hex dump format.

You can use float value similar to int. There is no big difference.

Takatoshi Kondo
  • 3,111
  • 17
  • 36
  • Thank you very much for helping. I had tried the same but I was getting restricted conversion or reduction from double to float compilation error. Now after seeing your reply, I tried again with double and it works. Could you also please guide me how can I get a grasp of msgpack? Documentation is very less as far as I have come across, so I struggle a lot in moving around. Thanks in advance. – Aparna Bose Nov 08 '18 at 12:32
  • Here is msgpack-c document https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_overview . The code works for me. I'm not sure about your error. – Takatoshi Kondo Nov 08 '18 at 12:41
  • Thanks a lot. You are a life-saver. – Aparna Bose Nov 08 '18 at 12:45
  • sir can we also store this hex dump into an array? When I try to do this, I get an incompatibility conversion error with ostream. – Aparna Bose Nov 13 '18 at 10:35
  • You can do that using sstream https://wandbox.org/permlink/zn0NKd0J3bB2m8gE . By the way, if your problem has been solved, could you check accept mark? – Takatoshi Kondo Nov 13 '18 at 12:47
  • Thank you..I tried again with string and ss.c_str() and got it that way as well :)..Yes I have accepted the answer but it says it has been recorded but I cannot make it public as I have less than 15 reputation. – Aparna Bose Nov 13 '18 at 14:08
  • Could you please help me with https://stackoverflow.com/questions/53544552/how-to-convert-a-tabular-format-or-python-dataframe-equivalent-format-to-msgpack – Aparna Bose Nov 29 '18 at 18:05