如果您查看所涉及的十六进制数字(成对)的值,您会发现最后 7 个字节在这两种情况下是相同的,但前 9 个字节稍微交换了一点。
从您的示例开始,但将 .NET 中的每一对重写为 00、11、22 等,并切换 Oracle 的相关字节,我们得到:
因此,编写代码来切换相关字节应该相当容易。 (事实上,我很确定我在之前的工作中编写了一些代码来执行此操作。)
要切换字节,您只需调用Guid.ToByteArray()
and new Guid(byte[])
回到一个Guid
.
编辑:碰巧的是,上面的切换回合是exactly什么Guid
当你向构造函数传递一个字节数组时,构造函数会执行以下操作:
using System;
using System.Linq;
class Test
{
static void Main()
{
byte[] bytes = Enumerable.Range(0, 16)
.Select(x => x * 16 + x)
.Select(x => (byte) x)
.ToArray();
Console.WriteLine(BitConverter.ToString(bytes).Replace("-", ""));
Console.WriteLine(new Guid(bytes).ToString().Replace("-", ""));
}
}
Prints:
00112233445566778899AABBCCDDEEFF
33221100554477668899aabbccddeeff
这很可能会使执行切换变得更加简单......您是如何开始获取这些值的?这只是“它们在 Oracle 中的显示方式”吗?
编辑:好的,这里有几个转换函数 - 如果您将数据作为文本,它们会以各种方式进行转换......
using System;
using System.Linq;
class Test
{
static void Main()
{
string oracle = "329DD817216CD6429B989F5201288DBF";
string dotNet = "17D89D326C2142D69B989F5201288DBF";
Console.WriteLine(oracle == DotNetToOracle(dotNet));
Console.WriteLine(dotNet == OracleToDotNet(oracle));
}
static string OracleToDotNet(string text)
{
byte[] bytes = ParseHex(text);
Guid guid = new Guid(bytes);
return guid.ToString("N").ToUpperInvariant();
}
static string DotNetToOracle(string text)
{
Guid guid = new Guid(text);
return BitConverter.ToString(guid.ToByteArray()).Replace("-", "");
}
static byte[] ParseHex(string text)
{
// Not the most efficient code in the world, but
// it works...
byte[] ret = new byte[text.Length / 2];
for (int i = 0; i < ret.Length; i++)
{
ret[i] = Convert.ToByte(text.Substring(i * 2, 2), 16);
}
return ret;
}
}