0

I currently have a *.cpp file where I import the needed *.proto.h file and instantiate an object based on one of the messages(Heartbeat) from my *.proto file. My goal is to use the set_value(), SerializeToString(), and ParseFromString().I've had success accomplishing the first two tasks. When using ParseFromString() not output is produced to the screen. I'm pretty new to protobufs so there may be something I'm overlooking. This is my code.

#include <iostream>
#include <fstream>
#include <string>
#include "cpnt.pb.h"

using namespace std;

int main()
{
    wombat::HeartBeat h;

    string m;
    string t;
    string v;

    h.set_value(1);
    v = h.SerializeToString(&m);
    t  = h.ParseFromString(v);

    cout << "\n The message received is:" << h.value();
    cout << "\n The binary data stored in v is:" << v;
    cout << "\n The parsed data stored in t is:" << t <<::endl;
    return 0;

}

And this is a printout of the output:

enter image description here

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32

1 Answers1

1

You misunderstood how SerializeToString works. h.SerializeToString(&m); serializes h and stores the result in m. h.ParseFromString(v); parsed from the string v to set members of h. Hence it should be

h.set_value(1);
h.SerializeToString(&m);
h.ParseFromString(m);

Both functions retun a bool, but I admit that I didn't find what it is good for in the documentation. Probably to signal an error.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • any idea on how to see the parsed message? I tried storing h.ParseFromString(m) to a variable and printing to the screen and it just returns binary – Tellrell White Sep 08 '21 at 16:20
  • @TellrellWhite `std::cout << m;` ? – 463035818_is_not_an_ai Sep 08 '21 at 17:35
  • @TellrellWhite That is because it is binary. It is not a text string, I have never understood why these functions are called this way when the use/produce an array of bytes. Best output the results as hex. – Bart Sep 08 '21 at 18:00
  • @Bart it took me a week of debugging before I realized that the seralized string can contain null terminators in the middle. Its really not nice to call it "string" – 463035818_is_not_an_ai Sep 08 '21 at 18:01
  • @Bart How exactly do you output in hex. And will I be able to see the message? – Tellrell White Sep 08 '21 at 18:41
  • @TellrellWhite You can convert it using `StringStream` objects: https://stackoverflow.com/questions/9893497/how-to-convert-byte-array-to-hexadecimal-string-in-c/27803436. You will be able to see the serialized message as hex format. So not the actual data you set in it but the serialized version of it. – Bart Sep 09 '21 at 06:36
  • `ParseFromString` returns true if the parsing has succeeded, false if not. It's meant for error checking. The `FromString`/`ToString` is somewhat confusing, but it just means that the functions deal with `std::string`, as opposed to [other methods](https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message_lite#parsing) like `ParseFromCodedStream` and `ParseFromArray`. It's important to note that it's perfectly valid for an `std::string` to contain null bytes. – Rafael Lerm Sep 09 '21 at 22:37
  • If you just want to see the values, you probably want `std::cout << h.DebugString()`. – Rafael Lerm Sep 09 '21 at 22:38