Converting a GUID string to octet string

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 )

6 thoughts on “Converting a GUID string to octet string

  1. 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. 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. 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. 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

  5. Here is the Delphi Way

    Function GUID2OctetString(GUIDstr:string):string;

    Function reversestring(s:string):string;
    var
    l:integer;
    tmpstr:string;
    begin
    while length(s)>2 do
    begin
    tmpstr:=tmpstr+copy(s,length(s)-1, length(s));
    s:=copy(s,1,length(s)-2);
    end;
    result:=tmpstr +s;
    end;

    var
    parcaA,ParcaB,ParcaC, Gerisi:string;
    OctetStr,tmp:string;
    i:integer;
    begin
    GUIDstr:=StringReplace(GUIDstr,'{‘,”,[rfReplaceAll, rfIgnoreCase]);
    GUIDstr:=StringReplace(GUIDstr,’}’,”,[rfReplaceAll, rfIgnoreCase]);

    ParcaA:=copy(GUIDstr,1,pos(‘-‘,GUIDstr)-1);
    GUIDstr:=copy(GUIDstr,pos(‘-‘,GUIDstr)+1, length(GUIDstr));

    ParcaB:=copy(GUIDstr,1,pos(‘-‘,GUIDstr)-1);
    GUIDstr:=copy(GUIDstr,pos(‘-‘,GUIDstr)+1, length(GUIDstr));

    ParcaC:=copy(GUIDstr,1,pos(‘-‘,GUIDstr)-1);
    GUIDstr:=copy(GUIDstr,pos(‘-‘,GUIDstr)+1, length(GUIDstr));

    Gerisi:=StringReplace(GUIDstr,’-‘,”,[rfReplaceAll, rfIgnoreCase]);
    Octetstr:= reversestring(ParcaA)+reversestring(ParcaB)+reversestring(ParcaC)+Gerisi;
    // 1,3,5 add back slashes
    while length(octetstr)>2 do
    begin
    tmp:=tmp+ ‘\’+ copy(octetstr,1,2);
    octetstr:=copy(octetstr,3,length(octetstr))
    end;
    result:=tmp+’\’+octetstr;
    end;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>