How to quickly generate UUID GUID

The structure of the universal GUID is as follows

typedef struct _GUID {

DWORD data 1;

WORD Data2

WORD Data3

Byte data 4 [8];

} GUID

For example:

Suppose the format of a GUID is 6b29fc40-ca47-1067-b31d-00dd010662da.

Among them, Data 1 is 32 bits, which can be regarded as eight four-digit hexadecimal numbers, corresponding to the above 6B29FC40.

Where Data2 is 16, which can be regarded as four four-digit hexadecimal numbers, corresponding to CA47 above.

Data3 is 16 bit, which can be regarded as four four-digit hexadecimal numbers, corresponding to 1067 above.

Among them, Data4 is special, 8 bytes, which can be regarded as 16 four-digit hexadecimal number.

Take data4 [0] and data4 [1] to form four four-digit hexadecimal numbers, corresponding to B3 1D above.

Take data4 [2] and data4 [3] to form four four-digit hexadecimal numbers, corresponding to the above 00DD.

Take data4 [4] and data4 [5] to form four four-digit hexadecimal numbers, corresponding to 0 106 above.

Take data4 [6] and data4 [7] to form four four-digit hexadecimal numbers, corresponding to the above 62DA.

* Note: Four hexadecimal digits correspond to one GUID character.

The function of generating GUID is provided under Windows. You need to use the header file "objbase.h" and the link library ole32.lib

HRESULT cocreate GUID(GUID * pguid);

Under Linux, you need to download the corresponding library files and header files first.

If you are a Debian user, you can easily get related resources through the apt command.

Apt-get installs uuid-dev

After the installation, you will find that

There is a uuid folder under /usr/include/, which contains the header file uuid.h

There are several libuuid* link library files under /usr/lib/, including static and dynamic link libraries.

Open uuid.h and you will find a function declaration of uuid _ uuid_generate(uuid_t out). We can generate UUID by calling this function.

uuid _ generate(reinterpret _ cast & lt; Unsigned character *> (& guid));

Is GUID easy to generate? Yes, because we don't need to implement the generated algorithm, we are standing on the shoulders of our predecessors, so we should thank them.

The following are general programs under WINDOWS and LINUX.

// uuid_test.cpp

# include & lt string & gt

# include & ltstdio.h & gt

# include & ltiostream & gt

# contains "uuid_test.hpp"

#ifdef WIN32

# include & ltobjbase.h & gt

# Otherwise

# include & ltuuid/uuid . h & gt;

#endif

Use namespace std

Namespace ChinuxTeam

{

GUID CreateGuid()

{

GUID guid

#ifdef WIN32

CoCreateGuid(& amp; guid);

# Otherwise

uuid _ generate(reinterpret _ cast & lt; Unsigned character *> (& guid));

#endif

Return guid

}

string GUID tostring(const GUID & amp; guid)

{

char buf[64]= { 0 };

#ifdef __GNUC__

snprintf(

# Otherwise // MSVC

_snprintf_s(

#endif

buf,

sizeof(buf),

" { % 08X-% 04X-% 04X-% 02X % 02X-% 02X % 02X % 02X % 02X % 02X % 02X } ",

guid。 Data 1, guid. Data 2, guid. Data 3,

guid。 Data 4[0], guid. Data 4[ 1],

guid。 Data 4[2], guid. Data 4[3],

guid。 Data 4[4], guid. Data 4[5],

guid。 Data 4[6], guid. data 4[7]);

Returns STD:: string (buf);

}

}

Corresponding header file

// uuid_test.hpp "

# include & lt string & gt

# include & ltstdio.h & gt

# include & ltiostream & gt

typedef struct _GUID

{

Unsigned long data1;

Unsigned short data 2;

Unsigned short data 3;

Unsigned character data 4 [8];

} UUID GUID;

Namespace ChinuxTeam

{

GUID create GUID();

string GUID tostring(const GUID & amp; guid);

}//namespace ChinuxTeam

The last GuidToString () is explained a little bit, which is used to output GUIDs as a standard String with a fixed format.

According to different calls of compiler, different string printing functions are called.

Attach a simple test function.

# include & lt string & gt

# include & ltstdio.h & gt

# include & ltiostream & gt

#ifdef WIN32

# contains "objbase.h"

# Otherwise

typedef struct _GUID

{

Unsigned long data1;

Unsigned short data 2;

Unsigned short data 3;

Unsigned character data 4 [8];

} UUID GUID;

#endif

Namespace ChinuxTeam{

GUID create GUID();

string GUID tostring(const GUID & amp; guid);

}

After running, a GUID will be generated and printed on the console with standard output.