Convert boost::uuid to char*

♀尐吖头ヾ 提交于 2019-12-03 14:29:31

问题


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


回答1:


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();



回答2:


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();



回答3:


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.)




回答4:


You use the stream functions in boost/uuid/uuid_io.hpp.

boost::uuids::uuid u;

std::stringstream ss;
ss << u;
ss >> u;



回答5:


boost::uuids::uuid u;

const char* UUID = boost::uuids::to_string(u).c_str();

It is possible to do a simple and quick conversion.



来源:https://stackoverflow.com/questions/3452272/convert-boostuuid-to-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!