How can I convert from unsigned short to string using C++? I have tow unsigned short variables:
unsigned short major = 8, minor = 1;
I want to join them for on string, looks like:
std::string version = major + "." + minor;
how can I do it? will aprrechiate a small sample code.
Thanks
could use std::stringstream or std::to_string(C++11) or boost::lexical_cast
#include<sstream>
std::stringstream ss;
ss << major << "." << minor;
std::string s = ss.str();
std::to_string:
std::string s = std::to_string(major) + "." +std::to_string(minor);
In C++11, you don't need some stream do do this:
std::string version = std::to_string(major)
+ "." + std::to_string(minor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With