不幸的是,您不能拥有相同的字节数组并生成Guid.ToString
与 Linux 字符串匹配的字符串。
您需要决定优先考虑哪一个:
var dotNetGuid = new Guid(new byte[] { 101, 208, 176, 173, 236, 192, 64, 86,
191, 214, 132, 2, 213, 232, 143, 247 });
// option 1 - keep the existing guid's byte array intact
// and create a new ToUnixString method to display it as-required
Console.WriteLine(dotNetGuid.ToString()); // adb0d065-c0ec-5640-bfd6-8402d5e88ff7
Console.WriteLine(dotNetGuid.ToUnixString()); // 65d0b0ad-ecc0-4056-bfd6-8402d5e88ff7
// option 2 - create a new guid by re-arranging the existing guid's byte array
// and then use the standard ToString method
var unixGuid = dotNetGuid.ChangeByteOrder();
Console.WriteLine(dotNetGuid.ToString()); // adb0d065-c0ec-5640-bfd6-8402d5e88ff7
Console.WriteLine(unixGuid.ToString()); // 65d0b0ad-ecc0-4056-bfd6-8402d5e88ff7
// ...
public static class GuidExtensions
{
public static string ToUnixString(this Guid guid,
string format = "D", IFormatProvider provider = null)
{
return guid.ChangeByteOrder().ToString(format, provider);
}
public static Guid ChangeByteOrder(this Guid guid)
{
var s = guid.ToByteArray();
var d = new byte[]
{
s[3], s[2], s[1], s[0], s[5], s[4], s[7], s[6],
s[8], s[9], s[10], s[11], s[12], s[13], s[14], s[15]
};
return new Guid(d);
}
}