1

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?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
rjoshi
  • 1,645
  • 1
  • 20
  • 31

1 Answers1

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

xgdgsc
  • 1,367
  • 13
  • 38
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • I already tried that but it doesn't copy ascii characters. it has some binary characters which I can't print. Event I tried to print in std::hex but still couldn't read/see. try printing using std::out. – rjoshi Feb 28 '11 at 01:01
  • See my edit, you can't represent UUID with 16 ASCII characters – CharlesB Feb 28 '11 at 06:40
  • I am not sure why would it add 4 separators but it does make sense to convert unsigned char to char would take 2 bytes so it requires 32. – rjoshi Feb 28 '11 at 18:12
  • @rjoshi it's the standard ASCII representation of a UUID (http://en.wikipedia.org/wiki/Uuid) – CharlesB Feb 28 '11 at 18:33