Guid to String

by ixmx


guid to string convert code snippet from Alex at experts-exchange

 
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
 
using namespace std;
 
string guid2string(GUID &guid)
{
	struct GUIDPrint
	{
		unsigned int   dw1;
		unsigned short w2;
		unsigned short w3;
		unsigned short w4;
		unsigned short w5;
		unsigned int   dw6;
	};
 
	GUIDPrint guidp;
	memcpy(&guidp, &guid, sizeof(GUIDPrint));
	ostringstream oss;
 
	oss << "{" << hex << setw(8) << setfill('0') << guidp.dw1
		<< "-" << hex << setw(4) << setfill('0') << guidp.w2
		<< "-" << hex << setw(4) << setfill('0')  << guidp.w3
		<< "-" << hex << setw(4) << setfill('0')  << guidp.w4
		<< "-" << hex << setw(4) << setfill('0')  << guidp.w5
		<< hex << setw(8) << setfill('0') << guidp.dw6 << "}";
	return oss.str();
}
Advertisements