字符串中boost uuid的大小返回36

时间:2011-02-27 23:44:18

标签: boost uuid boost-uuid

我正在尝试使用boost :: uuid生成16个字符的uuid字符串,但它返回36个字符。

boost::uuids::uuid uid == boost::random_generator()();
std::cout << size of uid:" << uid.size << std::endl; //always 16
std::stringstream ss;
ss<< uid;
std::string s = ss.str();
std::cout << "size of uid:" << s.size() << std::endl; // always 36

如何获得16个字符的uuid字符串?

1 个答案:

答案 0 :(得分:2)

根据the documentation,这段代码应该给你一个16个字符的字符串:

#include <boost/uuid/uuid.hpp>            // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp>         // streaming operators etc.
boost::uuids::uuid uid = boost::random_generator()();
std::string s(uid.size());
std::copy(u.begin(), u.end(), s.begin());

然而,它不是ASCII字符串而是字节字符串。由于ASCII可以表示具有2个十六进制字符的字节,因此ASCII中的UUID有32个字符加上4个分隔符,36。所以你已经有了正确的代码:)