Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert boost::uuid to char*

Tags:

c++

uuid

boost

I am looking to convert a boost::uuid to a const char*. What is the correct syntax for the conversion?

like image 870
SchwartzE Avatar asked Aug 10 '10 18:08

SchwartzE


3 Answers

Just in case, there is also boost::uuids::to_string, that works as follows:

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>

boost::uuids::uuid a = ...;
const std::string tmp = boost::uuids::to_string(a);
const char* value = tmp.c_str();
like image 94
SkorKNURE Avatar answered Oct 13 '22 06:10

SkorKNURE


You can do this a bit easier using boost::lexical_cast that uses a std::stringstream under the hood.

#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>

const std::string tmp = boost::lexical_cast<std::string>(theUuid);
const char * value = tmp.c_str();
like image 23
user192610 Avatar answered Oct 13 '22 07:10

user192610


You can include <boost/uuid/uuid_io.hpp> and then use the operators to convert a uuid into a std::stringstream. From there, it's a standard conversion to a const char* as needed.

For details, see the Input and Output second of the Uuid documentation.

std::stringstream ss;
ss << theUuid;

const std::string tmp = ss.str();
const char * value = tmp.c_str();

(For details on why you need the "tmp" string, see here.)

like image 27
Reed Copsey Avatar answered Oct 13 '22 07:10

Reed Copsey