Created
October 15, 2020 08:29
-
-
Save qijianpeng/1b59002029441361c23cf9934f1cdef9 to your computer and use it in GitHub Desktop.
Generate uuid by boost lib.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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