« Power Lines | Main | Killer hardware? »
Converting a GUID string to octet string
By Adam Bell | April 9, 2007
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.ps1
# 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 )
Topics: PowerShell | 4 Comments »
July 31st, 2009 at 17:18
Assuming 8-bit bytes, then the first 3 bytes would be 7147a8. I think step 2 would be better stated as:
Take the first three sub-strings and swap their bytes (octets) about the mid-point of the sub-string.
August 1st, 2009 at 09:43
Hi Craig,
Excellent point. I wrote this post some time ago, and reading back I see that it isn’t as clear as I thought it was.
Thanks for the feedback, and I’ll edit the post shortly to be much clearer :-)
Cheers,
Adam
September 9th, 2009 at 02:05
Hi Adam,
I just came across this old post while looking for a solution for this. I wasn’t totally satisfied with this because I didn’t want to have to write a custom function. A little more digging and I discovered this way to convert GUIDs:
[System.String]::Join(”,($guid.ToByteArray() | ForEach-Object {$_.ToString(‘x2′)}))
Just sharing in case you want another way to do this that doesn’t require the ReverseBytes function.
Kirk out.
September 9th, 2009 at 03:40
Hey Kirk.
I like it. Thanks for that. I still find a lot of the time my brain is still wired to think in VBScript. It’s getting there slowly :s
Cheers,
Adam