.NET Native GUID conversion

后端 未结 3 1508
萌比男神i
萌比男神i 2020-12-10 07:48

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.

相关标签:
3条回答
  • 2020-12-10 08:20

    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 讨论(0)
  • 2020-12-10 08:25

    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.

    0 讨论(0)
  • 2020-12-10 08:31

    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
    
    0 讨论(0)
提交回复
热议问题