GUID’s seem to pop up everywhere these days. Where ever there’s a need to uniquely identify an object there’s a good chance that the GUID fits the bill.
The two main formats that I’ve encountered them in in Active Directory and Windows Installer has been string, or binary octet string format. Now despite the fact they look quite different, they are in fact the same.
Here’s the process:
1) Strip out the hyphens
Looking at the remaining string in bytes (groupings of pairs) then
2) Reverse the order in the first 3 bytes
3) concatenate the remaining bytes with the reversed bytes.
for example:
7147a8de-129a-4edd-9533-83982050211f
becomes:
dea847719a12dd4e953383982050211f
And our sample code:
# Convert GUID to Octet String
# Each pair needs to be written in Hex in the format: 4 bytes-2 bytes-2 bytes-2 bytes-6 bytes
# The first 3 byte sequences are written in reverse.
# —————————————————————————————————
function ReverseBytes
# —————————————————————————————————
{
Param (
$StringOfBytes
)
for ($i=0; $i -lt $StringOfBytes.length; $i+=2)
{
$NuString= $StringOfBytes.substring($i,2)+$NuString
}
return $NuString
}
# —————————————————————————————————
function convertguid
# —————————————————————————————————
{
Param (
$guid
)
$partA, $PartB, $PartC, $TheRest = $guid.split(“-”)
return ( ReverseBytes $PartA ) + ( ReverseBytes $PartB ) `
+ ( ReverseBytes $PartC ) + $TheRest
}
# —————————————————————————————————
$a = [system.guid]::NewGuid().ToString()
write-host $a
convertguid ( $a )