Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

vb.net - .NET Native GUID conversion

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...