从Oracle的RAW(16)转换为.NET的GUID

reb*_*ard 37 c# oracle nhibernate guid s#arp-architecture

我在手动调试.NET应用程序时遇到困难,其中Guid值从.NET到Oracle不同.

  • C#读取的位置:
    • 17D89D326C2142D69B989F5201288DBF
  • Oracle读到:
    • 329DD817216CD6429B989F5201288DBF

我怎么能够手动调试,即从C#的GUID能够将该值粘贴到oracle查询中并获得正确的结果(反之亦然)?

Jon*_*eet 50

如果查看十六进制数字所涉及的值(成对),您可以看到最后7个字节在两种情况下都是相同的,但前9个字节有一些切换.

从你的例子开始,但是将.NET中的每一对重写为00,11,22等,并切换Oracle的相关字节,我们得到:

因此,编写代码以切换相关字节应该相当容易.(我很确定我在以前的工作中写过一些代码来实现这一点.)

要切换字节,你只需要调用Guid.ToByteArray()new Guid(byte[])返回到Guid.

编辑:由于它发生时,开关轮以上是准确的是什么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("-", ""));
    }
}
Run Code Online (Sandbox Code Playgroud)

打印:

00112233445566778899AABBCCDDEEFF
33221100554477668899aabbccddeeff
Run Code Online (Sandbox Code Playgroud)

这可能会使执行切换变得相当简单......你是如何掌握价值的?它只是"他们如何在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;
    }

}
Run Code Online (Sandbox Code Playgroud)


sam*_*ric 8

从Oracle存储和读取Guids时,我遇到了同样的问题.

Jon的答案对于查询是正确的,但如果您的应用程序需要存储和读取Oracle中的Guids,请使用此线程中的FlipEndian函数:

.NET Native GUID转换

Byte[] rawBytesFromOracle;
Guid dotNetGuid = new Guid(rawBytesFromOracle).FlipEndian();
Run Code Online (Sandbox Code Playgroud)

只有在从Oracle回读时才需要翻转.

写入Oracle时,正常使用Guid.ToByteArray().

我花了太多时间试图完成这个简单的任务.

史蒂夫


小智 5

如果需要将GUIDPL/SQL转换为RAW,可以使用以下函数:

/*
    CONVERT a GUID FORMAT in RAW(16)
    EX:
        guid    = 88c6a267-65d2-48d6-8da2-6f45e2c22726
        raw     = 67A2C688D265D6488DA26F45E2C22726
*/
FUNCTION GuidToRaw( guid IN VARCHAR2 ) RETURN RAW
IS
    ret         RAW(16);
    guidHex     VARCHAR2(64);
BEGIN

    guidHex := SUBSTR (guid, 7, 2);
    guidHex := CONCAT( guidHex, SUBSTR (guid, 5, 2) );
    guidHex := CONCAT( guidHex, SUBSTR (guid, 3, 2) );
    guidHex := CONCAT( guidHex, SUBSTR (guid, 1, 2) );

    guidHex := CONCAT( guidHex, SUBSTR (guid, 12, 2) );
    guidHex := CONCAT( guidHex, SUBSTR (guid, 10, 2) );

    guidHex := CONCAT( guidHex, SUBSTR (guid, 17, 2) );
    guidHex := CONCAT( guidHex, SUBSTR (guid, 15, 2) );

    guidHex := CONCAT( guidHex, SUBSTR (guid, 20, 2) );
    guidHex := CONCAT( guidHex, SUBSTR (guid, 22, 2) );

    guidHex := CONCAT( guidHex, SUBSTR (guid, 25, 12) );

    ret := HEXTORAW( guidHex );

    return ret;

end;
Run Code Online (Sandbox Code Playgroud)