boostuuidboost-uuid

size of boost uuid in string returns 36


I am trying to generate 16 character uuid string using boost::uuid but it returns 36 characters.

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

How do I get 16 character uuid string?


Solution

  • 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 :)