从 Oracle 的 RAW(16) 转换为 .NET 的 GUID [英] Convert from Oracle's RAW(16) to .NET's GUID

查看:17
本文介绍了从 Oracle 的 RAW(16) 转换为 .NET 的 GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having difficulties manually debugging an .NET application where the Guid values differ from .NET to Oracle.

  • 其中 C# 读作:
    • 17D89D326C2142D69B989F5201288DBF
    • 329DD817216CD6429B989F5201288DBF

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

    How would I be able to manually debug, i.e., from C#'s GUID be able to paste that value in an oracle query and get the correct results (and viceversa)?

    推荐答案

    如果您查看所涉及的十六进制数字(成对)的值,您会发现这两种情况的最后 7 个字节相同,但前 9 个字节稍微切换了一下.

    If you look at the values involved (in pairs) of hex digits you can see that the last 7 bytes are the same in both cases, but the first 9 are switched around a bit.

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

    Going from your example, but rewriting each pair in the .NET as 00, 11, 22 etc and switching the relevant byte of Oracle as well we get:

    • .NET:

    • .NET:

    00112233445566778899AABBCCDDEEFF
    

  • 甲骨文:

  • Oracle:

    33221100554477668899AABBCCFFEEFF
    

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

    So it should be fairly easy to write code to switch round the relevant bytes. (I'm pretty sure I wrote some code to do this in a previous job, in fact.)

    要切换字节,您只需调用 Guid.ToByteArray()new Guid(byte[]) 即可返回 指导.

    To switch round the bytes, you'll just want to call Guid.ToByteArray() and new Guid(byte[]) to get back to a Guid.

    碰巧,上面的切换轮正是当你传递一个字节数组时Guid构造函数所做的:

    As it happens, the switch-round above is exactly what the Guid constructor does when you pass it a byte array:

    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("-", ""));
        }
    }
    

    打印:

    00112233445566778899AABBCCDDEEFF
    33221100554477668899aabbccddeeff
    

    这很可能使执行切换变得更加简单……您是如何掌握这些值的?仅仅是它们在 Oracle 中的显示方式"吗?

    That may well make it considerably simpler to perform the switching... how were you getting hold of the values to start with? Is it just "how they're displayed in Oracle"?

    好的,这里有几个转换函数 - 如果您将数据作为文本,它们会以各种方式进行转换...

    Okay, here are a couple of conversion functions - if you've got the data as text, they'll convert each way...

    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;
        }
    
    }
    

    这篇关于从 Oracle 的 RAW(16) 转换为 .NET 的 GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆