RSS All Posts

RSS PowerShell Posts

Tags

2142 Active Directory Administrativia Announcements Battlefield Blogging Cricket Deployment Deployment4 Get-PSUGUK Group Policy HowTo Linux MDT MDT 2010 Microsoft Deployment Toolkit MSDN Music Permissions Personal PowerGui Power Lines PowerShell PowerShell Groups PowerShell Support PowerShell Tools PowerShell V2 Presentations PSUGAU Quick Tips Scripting SDDL Security Tech Talk Ubuntu User Groups Virtualisation VMware Infrastructure Client WAIK Weekly Poll Windows 7 Windows Automation Installation Kit Windows Server 2003 Windows Server 2008 XML

Archives

Meta


« | Main | »

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 »

4 Responses to “Converting a GUID string to octet string”

  1. Craig Says:
    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.

  2. Adam Bell Says:
    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

  3. Poshoholic Says:
    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.

  4. Adam Bell Says:
    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

Comments