I have a simple subscriber to a topic in C++, and am receiving a custom PositionMsg, which includes other custom messages (Velocity, datum, etc). ROS2 handles all that very well, I'm getting the information.

Now I have been told to serialize the message as a string and store it in a database, where it will need to be deserialized later. Is there something in ROS2 that can write it out as a string. I can use Serialization and get back a SerializedMessage, but how do I save that as a std::string? It does not need to be easily human readable as a string, but if it is easier to do in yaml or json, that is fine too.


1 Answer 1


I think this is just an array to string problem, there's an array of uint8_t inside the SerializedMessage wrapper:

auto message = std_msgs::msg::String();
message.data = "Hello, world!"; 

rclcpp::Serialization<std_msgs::msg::String> stringSerializer;
rclcpp::SerializedMessage serialized_msg;
stringSerializer.serialize_message(&message, &serialized_msg);
const auto buffer_begin = serialized_msg.get_rcl_serialized_message().buffer;
const auto buffer_end = buffer_begin + serialized_msg.size();
std::string serialized_msg_str(buffer_begin, buffer_end);

https://stackoverflow.com/questions/17982752/convert-vector-of-uint8-to-string and also using https://stackoverflow.com/questions/77315367/how-to-implement-deserialization-of-ros2-messages


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.