将C#函数转换为等效的SQL函数

时间:2015-03-30 13:32:35

标签: c# sql-server

我有一个c#函数,如下所示。

public double myFunction(byte[] hashA, byte[] hashB)
    {
        byte[] D = new byte[72];
        for (int i = 0; i < 72; i++)
        {
            D[i] = (byte)(hashA[i] ^ hashB[i]);
        }
        double bits = (double)72 * 8;
        double magic = //Snipped code using D
        double k = (magic / bits);
        return (100 - (100 * k)); ;
    }

我想将其转换为等效的SQL Server函数。我已经编写了如下所示的功能。但是,我坚持转换以下一行

 D[i] = (byte)(hashA[i] ^ hashB[i]); 

MY SQL功能如下所示:

CREATE function [dbo].[MyFunction](@hash1 binary(72), @hash2 binary(72))
returns int

AS
BEGIN 
    DECLARE  @dist float, @D binary(72)
     DECLARE @i INT
     DECLARE @bits float, @k float
      SET @i = 0
      SET @i = 0
     WHILE @i <= 72
    BEGIN
        SET @D[i] = CAST((@hash1[i] ^ @hash2[i]) AS BINARY)

        SET @i = @i + 1
            END

    END

1 个答案:

答案 0 :(得分:0)

你可以使用这样的东西。

DECLARE @hash1Byte binary(1)
    DECLARE @hash2Byte binary(1)
    DECLARE @d binary(1)
    SET @i = 0
    WHILE @i < 72
    BEGIN
        SET @i = @i + 1

        set @hash1Byte = substring(@hash1, @i, 1)
        set @hash2Byte = substring(@hash2, @i, 1)
        set @d = @hash1Byte ^ @hash2Byte

    END