C++ 将 boost::uuid 转换为 char*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3452272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:52:40  来源:igfitidea点击:

Convert boost::uuid to char*

c++boostuuid

提问by SchwartzE

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

我希望将 boost::uuid 转换为 const char*。转换的正确语法是什么?

回答by SkorKNURE

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

以防万一,还有boost::uuids::to_string,其工作原理如下:

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

回答by user192610

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

你可以使用 boost::lexical_cast 更容易地做到这一点,它在引擎盖下使用 std::stringstream 。

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

回答by Reed Copsey

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.

您可以包含<boost/uuid/uuid_io.hpp>然后使用运算符将​​ uuid 转换为std::stringstream. 从那里,它是const char*根据需要标准转换为 a 的。

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

有关详细信息,请参阅Uuid 文档的输入和输出第二部分

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

(有关为什么需要“tmp”字符串的详细信息,请参见此处。)

回答by Joe

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

您可以使用 boost/uuid/uuid_io.hpp 中的流函数。

boost::uuids::uuid u;

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

回答by Matheus Toniolli

boost::uuids::uuid u;

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

It is possible to do a simple and quick conversion.

可以进行简单快速的转换。