I have converted a few numbers to a string with a separator from type of string
as follows
#include <sstream>
#include <string>
#include <iostream>
std::string vars_to_string(std::string separator, double object_x, double object_y, double object_z, std::string msg) {
std::stringstream ss;
ss << msg << separator;
ss << object_x << separator;
ss << object_y << separator;
ss << object_z;
return ss.str();
}
void string_to_vars(std::string text, std::string separator, double &object_x, double &object_y, double &object_z, std::string &msg) {
// ????????
}
int main() {
double x = 4.2, y = 3.7, z = 851;
std::string msg = "position:";
std::string text = vars_to_string("__%$#@!__", x, y, z, msg);
std::cout << text << std::endl;
double rx, ry, rz;
std::string rmsg;
string_to_vars(text, "__%$#@!__", rx, ry, rz, rmsg);
std::cout << "Retrived data: " << rx << ", " << ry << ", " << rz << ", " << rmsg << std::endl;
return 0;
}
Now, I wonder how I can do the reverse to convert the result variable into msg
, object_x
, object_y
, object_z
again?
To stop suggesting trivial solutions I use __%$#@!__
as a separator. The separator is decided by the user and it can be any arbitrary value and the code should not fail.
I do not make assumption that object_x
is pure digits. I only assume that it does not contain the separator.
We do not know the separator.
Is there any simple solution without using Boost?