1

我正在尝试使用 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 字符串?

4

1 回答 1

2

According to the documentation, this piece of code should give you a 16 character string:

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

However it's not an ASCII string but a byte string. As ASCII can represent bytes with 2 hex characters, UUID in ASCII have 32 characters plus 4 separators, 36. So you already have the right code :)

于 2011-02-27T23:52:34.040 回答