Skip to content

Instantly share code, notes, and snippets.

@qijianpeng
Created October 15, 2020 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qijianpeng/1b59002029441361c23cf9934f1cdef9 to your computer and use it in GitHub Desktop.
Save qijianpeng/1b59002029441361c23cf9934f1cdef9 to your computer and use it in GitHub Desktop.
Generate uuid by boost lib.
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/lexical_cast.hpp>
std::string Genuuid(const int startPos, const int lengthHex)
{
boost::uuids::random_generator gen;
boost::uuids::uuid uuidId = gen();
std::string randomUUID = boost::lexical_cast<std::string>(uuidId);
std::remove( randomUUID.begin(), randomUUID.end(), '-');
randomUUID = "0x" + randomUUID.substr(startPos, lengthHex);
return randomUUID;
}
uint64_t Gen64Uuid()
{
uint64_t ui64 = 0;
const int startPosition = 18;//can vary - we can use rand() too
const int lengthHex = 14;//can vary - we can use rand() too
std::string randomUUID = Genuuid(startPosition, lengthHex);
ui64 = std::stoull(randomUUID, 0, 16); //random out of UUID
return ui64;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment