6

I have an external database that is feeding information to me. One saves their data as native GUID format and my other data source supplies standard .NET GUID format string.

Is there a tidy way to convert from Native GUID to GUID Structure?

Also is there any validation bit to determine if a provided value is a Native GUID or not? I can't seem to find any if there is one.

The difference is as follows:

typedef struct _GUID 
{  
   DWORD Data1;  
   WORD Data2;  
   WORD Data3;  
   BYTE Data4[8];
} GUID;

Data1, Data2 and Data3 get their byte order reversed but Data4 remains the same, see http://en.wikipedia.org/wiki/Globally_unique_identifier for more info

4
  • 1
    Sorry, clueless - what's the difference? Isn't a GUID just 16 bytes of data either way?
    – Rup
    Nov 9, 2011 at 11:58
  • 1
    The first 8 bytes of a GUID can change their byte endianness, but there doesn't seem to be a clean way in .NET to accept data that has this reversed order. For more info: stackoverflow.com/questions/1644989/…
    – Seph
    Nov 9, 2011 at 12:03
  • Thanks, I'd never seen that. I'd assumed the endianness was mandated like IP stuff. I think it'd be simplest to write your own parser for the hex string for the native case then.
    – Rup
    Nov 9, 2011 at 12:07
  • 1
    Bit order reversal seems very unlikely. Byte order is possible but still a stretch. Ultimately it is up to the data provider you use to make this conversion. Contact the vendor for support. Don't discount the possibility of a bug in whatever viewer you use to look at the dbase table. Nov 9, 2011 at 12:44

3 Answers 3

17

To see if the input is in little Endian or not BitConverter.IsLittleEndinan() helps.

I just had to do this same thing and using Paul Smith's answer above I got it working with this code. Derived off his code but with a fix on last byte swap order and condensed to one flip ensuring guid.FlipEndian().FlipEndian() == guid.

C# Code:

public static class Extensions
{
    /// <summary>
    /// A CLSCompliant method to convert a big-endian Guid to little-endian
    /// and vice versa.
    /// The Guid Constructor (UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte,
    ///  Byte, Byte, Byte, Byte) is not CLSCompliant.
    /// </summary>
    [CLSCompliant(true)]
    public static Guid FlipEndian(this Guid guid)
    {
        var newBytes = new byte[16];
        var oldBytes = guid.ToByteArray();

        for (var i = 8; i < 16; i++)
            newBytes[i] = oldBytes[i];

        newBytes[3] = oldBytes[0];
        newBytes[2] = oldBytes[1];
        newBytes[1] = oldBytes[2];
        newBytes[0] = oldBytes[3];
        newBytes[5] = oldBytes[4];
        newBytes[4] = oldBytes[5];
        newBytes[6] = oldBytes[7];
        newBytes[7] = oldBytes[6];

        return new Guid(newBytes);
    }
}

VB.net code (Translated from online service):

Imports System.Runtime.CompilerServices

Module ModuleExtension

    ''' <summary>
    ''' A CLSCompliant method to convert a big-endian Guid to little-endian
    ''' and vice versa.
    ''' The Guid Constructor (UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte,
    '''  Byte, Byte, Byte, Byte) is not CLSCompliant.
    ''' </summary>
    <Extension()>
    Public Function FlipEndian(guid As Guid) As Guid
        Dim newBytes = New Byte(15) {}
        Dim oldBytes = guid.ToByteArray()

        For i As Integer = 8 To 15
            newBytes(i) = oldBytes(i)
        Next

        newBytes(3) = oldBytes(0)
        newBytes(2) = oldBytes(1)
        newBytes(1) = oldBytes(2)
        newBytes(0) = oldBytes(3)
        newBytes(5) = oldBytes(4)
        newBytes(4) = oldBytes(5)
        newBytes(6) = oldBytes(7)
        newBytes(7) = oldBytes(6)

        Return New Guid(newBytes)
    End Function

End Module
5
  • 1
    I had the same problem pulling Binary(16) UUID from MySQL using LINQ (the latter 2 parts of the GUID were as expected, but the first 2 snippets were different). This fcn restores my sanity. Thanks.
    – sobelito
    Jun 11, 2015 at 20:32
  • @Maverik Are you sure about the order? I think it is: 3 2 1 0 5 4 7 6 Aug 23, 2017 at 17:43
  • @Andres it's been working for me since I posted this. I literally just used it five minutes back to query active directory. Its been too long since I read the theory of this and can't remember the flip order OTOMH.
    – Maverik
    Aug 23, 2017 at 18:36
  • @Andres - I think, in essence, it is as you suggest. If you look at the array subscripts for oldBytes, it goes .... 5 7 6 which would result in what you are expecting anyway.
    – Mr Moose
    Dec 14, 2017 at 17:24
  • much simpler when using Array.Reverse() as ``` var bytes = guid.ToByteArray(); Array.Reverse(bytes, 0, 4); Array.Reverse(bytes, 4, 2); Array.Reverse(bytes, 6, 2); return bytes; ```
    – hIpPy
    May 25, 2021 at 20:45
1

If I understand the question correctly, I posted extension methods that do this in How to read a .NET Guid into a Java UUID.

0

If in fact you are dealing with Endian issues, you'll have no choice but to parse the string yourself into the constituent parts of the Guid switch the Endianness, and then create a Guid that you would then use.

1
  • I suppose I'll have to write my own wrapper to reverse the values
    – Seph
    Nov 9, 2011 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.