要查看輸入是否在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
對不起,沒有線索 - 有什麼區別?這兩種方式不僅僅是一個GUID只有16個字節的數據? – Rup
GUID的前8個字節可以改變它們的字節字節順序,但在.NET中似乎沒有一種乾淨的方式來接受具有相反順序的數據。欲瞭解更多信息:http://stackoverflow.com/questions/1644989/difference-between-nativeguid-and-guid-in-active-directory – Seph
謝謝,我從來沒有見過。我假設序列號是像IP的東西一樣強制執行的。我認爲編寫自己的解析器爲本地情況下的十六進制字符串是最簡單的。 – Rup