Convert binary to UUID from LDAP

Alexander Zhirov azhirov1991 at gmail.com
Tue Mar 28 08:15:03 UTC 2023


On Tuesday, 28 March 2023 at 05:26:08 UTC, Alexander Zhirov wrote:
> When converting to HEX, I get the string 
> `121F4C264DED5E41A33F445B0A1CAE32`, in which some values are 
> reversed. I found ways on the Internet to transform the 
> permutation method into the desired result, but most likely it 
> will be a crutch rather than the right solution to lead to the 
> final result `264c1f12-ed4d-415e-a33f-445b0a1cae32`.

So far it has been possible to convert like this

```d
{
     writeln(value.attributes["objectGUID"][0].toUUID);
}

UUID toUUID(const char[] objectGUID)
{
     if(objectGUID.length != 16)
         throw new Exception("objectGUID does not match the 
length");

     auto arr = (cast(ubyte[])objectGUID).array;

     auto part1 = arr[0 .. 4].reverse;
     auto part2 = arr[4 .. 6].reverse;
     auto part3 = arr[6 .. 8].reverse;
     auto part4 = arr[8 .. 10];
     auto part5 = arr[10 .. $];

     string hex =
         toHexString(part1) ~ '-' ~
         toHexString(part2) ~ '-' ~
         toHexString(part3) ~ '-' ~
         toHexString(part4) ~ '-' ~
         toHexString(part5);

     return cast(UUID)hex;
}
```


More information about the Digitalmars-d-learn mailing list