2 years ago
#365367

Viktor Khristenko
msgpack c++ packing and go unpacking
I was trying out msgpack, with c++ and go versions, however I run into a problem of not being able to produce identical binaries when marshalling/packing data...
here is the cpp version
struct Data {
    int X, Y, Z;
    std::string Msg;
    double Value1, Value2;
    MSGPACK_DEFINE(X, Y, Z, Msg, Value1, Value2);
};
void test0() {
    Data data {
        1, 1, 1,
        std::string{"value"},
        1.1, 2.2
    };
    std::stringstream buf;
    ::msgpack::pack(buf, data);
    auto str = buf.str();
    fmt::print("size = {}\n", str.size());
    for (int i=0; i<str.size(); i++) {
        if (i % 20 == 0) fmt::print("\n");
        fmt::print("{0:02x} ", (unsigned char)str[i]);
    }
    fmt::print("\n");
}
here is the go version
package main
import (
    "fmt"
    msgpack "github.com/vmihailenco/msgpack/v5"
)
type Data struct {
    X, Y, Z int 
    Msg string
    Value1, Value2 float64
}
func main() {
    bytes, err := msgpack.Marshal(&Data {
        X: 1,
        Y: 1,
        Z: 1,
        Msg: "value",
        Value1: 1.1,
        Value2: 2.2,
    })  
    if err != nil {
        panic(err)
    }   
    fmt.Println(len(bytes))
    for i, b := range bytes {
        if i % 20 == 0 { 
            fmt.Println()
        }   
        fmt.Printf("%02x ", b)
    }   
    fmt.Println()
    var data Data
    err = msgpack.Unmarshal(bytes, &data)
    if err != nil {
        panic(err)
    }   
    fmt.Println(data)
}
Dumping arrays of bytes for both cases i get different results. I suspect i'm doing something wrong... but i do not quite see it
VK
c++
go
msgpack
0 Answers
Your Answer