Hi
im looking to create a structured string in c++ the structure would be simular to a guid or uuid but programatically without using the guid apis, could you help me acheive this
Thanks,
Nathaniel Blackburn
Hi
im looking to create a structured string in c++ the structure would be simular to a guid or uuid but programatically without using the guid apis, could you help me acheive this
Thanks,
Nathaniel Blackburn
just call rand() to generate random characters between 48 to 57 and 65 to 90 (see any ascii chart)
You can also fill a std::string
with all the valid characters you wish to support and generate random indexes into that string. Something like
std::string bucket = "abcdefghijklmnopqrstuvwxyz";
std::string uuid;
for ( ... ) {
uuid += bucket[rand() % bucket.size()];
}
Thanks guys but how would you split the steing into blocks such as; xxxxx-xxxxx-xxxxx-xxxxx-xxxxx where x represents a character
The format of that string is enough to make me not want to reply without further information. What is the purpose of you wanting to generate a uuid in that particular format?
The format of that string is enough to make me not want to reply without further information. What is the purpose of you wanting to generate a uuid in that particular format?
I caught on to the message hiding behind your reply straight away and don't worry its not for malicious intent, i would never dream of doing something like that, its actually just for creating folders and files with a token styled directory like in the temporary directory in windows, the structure would actually be more like the example below to make the files that are placed there less obvious to find as i was going to install my software protection control into that folder rather than a obvious one before it was activated and then delete it again afterwards, its basically to keep my files more secure from reverse engineering and hackers.
{0E148812-AAEB-4B19-8A1D-D2AA21CD1355}
Thanks,
Nathaniel Blackburn
The format of that string is enough to make me not want to reply without further information. What is the purpose of you wanting to generate a uuid in that particular format?
UUIDs are in that format, and are used a lot in MS-Windows programming to uniquely identify objects. Just look in the registry and you will find millions of them.
Thanks guys but how would you split the steing into blocks such as; xxxxx-xxxxx-xxxxx-xxxxx-xxxxx where x represents a character
That's quite simple -- add the dashes when you generate the characters.
std::string uuid;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
uuid += GenerateRandomDigit();
}
uuid += '-';
}
UUIDs are in that format, and are used a lot in MS-Windows programming to uniquely identify objects. Just look in the registry and you will find millions of them.
That's quite simple -- add the dashes when you generate the characters.
std::string uuid; for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { uuid += GenerateRandomDigit(); } uuid += '-'; }
Awesome thanks dude :)
Are there additional includes or changes i have to add as i'm making this into a dll as ones desperately needed, thanks again :)
UUIDs are in that format, and are used a lot in MS-Windows programming to uniquely identify objects. Just look in the registry and you will find millions of them.
Point taken, but realize that I'm no Windows programmer so that format was, to me, associated with product keys.
Yes product keys are only one use for them. I've even seen UUIDs used as part of code guards in header files.
Point taken, but realize that I'm no Windows programmer so that format was, to me, associated with product keys.
no worrys, im trying to make this source into a dll now, looking at the source to creates 5 blocks of 5 characters i think...
how would you make the following structure...
XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXX
and return it into a string as that's a little more tricky as that's the part im not so sure about doing but im starting to get the idea of c++ however i still prefer lua...
pass the destination string by reference to the DLL function.
// dll function
// assumes the application program has allocated memory for the string.
void foo(std::string& s)
{
int k = 0;
for(int i = 0; i < 5; i++)_
{
for(int j= 0; j < 5; j++)
s[k++] = GenrateRandomDigit();
if(i < 5)
s[k++] = '-';
}
}
// application program
int main()
{
std::string s;
// allocate memory for the string
s.reserve(30);
foo(s);
}
the last dll i had made was static and that is the kind i would need in order for this to work, here is a idea of what i mean with the source from my last project which was a simple dll that creates timestamps...
#include "timestamp.h"
BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
{
return TRUE;
}
DLL_EXPORT char* codestamp(void)
{
time_t now;
struct tm *ts;
static char buf[80];
now = time(0);
ts = localtime(&now);
strftime(buf, sizeof(buf), "%d%m%Y%H%M%S", ts);
return buf;
}
So you are looking for something that can adjust the field width? How about
void gen_uuid (std::string& dest, const std::vector< int >& format) {
static const std::string bucket = "0123456789ABCDEF";
std::vector< int >::const_iterator it = format.begin ();
while (it != format.end ()) {
if (it != format.begin ()) { dest += '-'; }
for (::size_t i = 0; i < *it; ++i)
dest += bucket[rand () % bucket.size ()];
++it;
}
}
WHere format
contains the widths of the fields you want to generate. In your example it would contain [8,4,4,12].
Again, I'm no Windows guy, so if the memory must be allocated before the call to the DLL you will need to take AncientDragon's suggestion and reserve before the call.
So you are looking for something that can adjust the field width? How about
void gen_uuid (std::string& dest, const std::vector< int >& format) { static const std::string bucket = "0123456789ABCDEF"; std::vector< int >::const_iterator it = format.begin (); while (it != format.end ()) { if (it != format.begin ()) { dest += '-'; } for (::size_t i = 0; i < *it; ++i) dest += bucket[rand () % bucket.size ()]; ++it; } }
WHere
format
contains the widths of the fields you want to generate. In your example it would contain [8,4,4,12].Again, I'm no Windows guy, so if the memory must be allocated before the call to the DLL you will need to take AncientDragon's suggestion and reserve before the call.
this seems to be more like what im after, how do you return the string that has been created from that function, thanks
#include "guidgen.h"
BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
{
return TRUE;
}
DLL_EXPORT void guidgen(std::string& dest, const std::vector< int >& format)
{
static const std::string bucket = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::vector< int >::const_iterator it = format.begin ();
while (it != format.end ()) {
if (it != format.begin ()) { dest += '-'; }
for (::size_t i = 0; i < *it; ++i)
dest += bucket[rand () % bucket.size ()];
++it;
}
}
}
#ifndef _DLL_H_
#define _DLL_H_
#include <windows.h>
#include <stdio.h>
#ifndef DLL_EXPORT
#define DLL_EXPORT extern "C" __declspec(dllexport)
#endif
DLL_EXPORT void guidgen(std::string& dest, const std::vector< int >& format);
#endif
EDIT: my code doesn't appear to work, could you help as i'm gutted :(
> Point taken, but realize that I'm no Windows programmer
> so that format was, to me, associated with product keys.
uuids are not a Windows innovation; they conform to the standard originally specified by OSF/DCE and now in ISO/IEC 11578:1996. Windows uses uuids; but so do several other systems - Linux ext2/ext3 file systems, Mac OS X, GNOME, KDE, J2SE among others.
For example, using libuuid:
#include <string>
#include <uuid/uuid.h>
std::string rand_uuid_string() // link with -luuid
{
uuid_t uuid ;
uuid_generate_random( uuid ) ;
char temp[64] ;
uuid_unparse( uuid, temp ) ;
return temp ;
}
The current release of boost has a portable implementation:
#include <boost/uuid/random_generator.hpp>
#include <string>
std::string rand_uuid_string()
{
static boost::uuids::random_generator generator ;
return generator().to_string() ;
}
uuids are not a Windows innovation; they conform to the standard originally specified by OSF/DCE and now in ISO/IEC 11578:1996. Windows uses uuids; but so do several other systems - Linux ext2/ext3 file systems, Mac OS X, GNOME, KDE, J2SE among others.
And...?
Wikipedia suggest the canonical format is: 8-4-4-4-12 with an example of 550e8400-e29b-41d4-a716-446655440000
whereas the Windows product key format is: 5-5-5-5-5. I saw the latter of the two and asked for clarification.
I'm pretty sure this has been cleared up at this point.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.