Skip to content

Instantly share code, notes, and snippets.

@mjac
Created February 19, 2014 11:56
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 mjac/9090539 to your computer and use it in GitHub Desktop.
Save mjac/9090539 to your computer and use it in GitHub Desktop.
Creates a C++ GUID structure from a source string
// Creates a C++ GUID structure from a source string
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373931(v=vs.85).aspx
/*
typedef struct _GUID {
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
} GUID;
*/
// ConvertGuidStringToCppStructure('490A7178-3239-43F1-9B34-2049EBABB6A8');
// " { 0x490A7178, 0x3239, 0x43F1, { 0x9B, 0x34, 0x20, 0x49, 0xEB, 0xAB, 0xB6, 0xA8 } } "
// Requires my other Gist "GroupCharacters"
function ConvertGuidStringToCppStructure(guidString)
{
var cleanGuid = guidString.replace(/[^a-zA-Z0-9]/g, '');
function GroupGuid(cleanGuid)
{
return [
cleanGuid.substring(0, 8),
cleanGuid.substring(8, 12),
cleanGuid.substring(12, 16),
GroupCharacters(cleanGuid.substring(16, 32), 2)
];
}
function AddHexIndication(b)
{
if(typeof b === 'string')
{
return '0x' + b;
}
else
{
return ' { ' + b.map(AddHexIndication).join(', ') + ' } ';
}
}
var groupedGuid = GroupGuid(cleanGuid);
return AddHexIndication(groupedGuid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment