2011-11-09 35 views
4

我有一個向我提供信息的外部數據庫。一個將它們的數據保存爲本地GUID格式,另一個數據源提供標準的.NET GUID格式字符串。.NET本地GUID轉換

是否有一種整潔的方式將Native GUID轉換爲GUID Structure

還有任何驗證位來確定提供的值是否是本地GUID?我似乎無法找到任何有一個。

的區別如下:

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

數據1,數據2和數據3得到他們的字節順序顛倒,但數據4是一樣的,看http://en.wikipedia.org/wiki/Globally_unique_identifier更多信息

+1

對不起,沒有線索 - 有什麼區別?這兩種方式不僅僅是一個GUID只有16個字節的數據? – Rup

+1

GUID的前8個字節可以改變它們的字節字節順序,但在.NET中似乎沒有一種乾淨的方式來接受具有相反順序的數據。欲瞭解更多信息:http://stackoverflow.com/questions/1644989/difference-between-nativeguid-and-guid-in-active-directory – Seph

+0

謝謝,我從來沒有見過。我假設序列號是像IP的東西一樣強制執行的。我認爲編寫自己的解析器爲本地情況下的十六進制字符串是最簡單的。 – Rup

回答

0

事實上,如果你正在處理的字節序問題,你別無選擇,只能自己解析字符串Guid的組成部分,然後切換Endianness,然後使用create a Guid

+0

我想我將不得不寫我自己的包裝來扭轉值 – Seph

7

要查看輸入是否在little endian或BitConverter.IsLittleEndinan()幫助。

我只是不得不做同樣的事情,並使用上面的Paul Smith的答案我得到它與這段代碼一起工作。派生出他的代碼,但修正了最後一個字節交換順序並壓縮到一個翻轉保證guid.FlipEndian()。FlipEndian()== guid。

C#代碼:

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代碼(從在線服務翻譯):

Public NotInheritable Class Extensions 
    Private Sub New() 
    End Sub 
    ''' <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)> _ 
    <System.Runtime.CompilerServices.Extension> _ 
    Public Shared Function FlipEndian(guid As Guid) As Guid 
     Dim newBytes = New Byte(15) {} 
     Dim oldBytes = guid.ToByteArray() 

     For i As var = 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 Class 
+1

我有同樣的問題從MySQL使用LINQ拉二進制(16)UUID(GUID的後兩部分是預期的,但前2個片段是不同)。這個fcn恢復了我的理智。謝謝。 – sobelito

+0

@Maverik你確定訂單嗎?我認爲它是:3 2 1 0 5 4 7 6 – Andres

+0

@Andres自從我發佈這個以來,它一直在爲我工作。我簡直就是用五分鐘時間回來查詢活動目錄。自從我讀了這個理論並且不記得翻轉順序OTOMH以來,它太久了。 – Maverik