Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取Sql Server 2008中Guid.getHashCode()的等效值_C#_Asp.net_Sql_Sql Server 2008_Asp.net 2.0 - Fatal编程技术网

C# 获取Sql Server 2008中Guid.getHashCode()的等效值

C# 获取Sql Server 2008中Guid.getHashCode()的等效值,c#,asp.net,sql,sql-server-2008,asp.net-2.0,C#,Asp.net,Sql,Sql Server 2008,Asp.net 2.0,我正在尝试获取SQL Server 2008中guid.getHashCode()的等效int值。我尝试了校验和(uniqueidentifier var),但它没有返回相同的值。是否可以在SQL Server中获取guid的哈希代码值 比如说 Guid guid = new Guid("A0AE0446-3C50-479A-A909-3BA9C711007E"); int hash = guid.GetHashCode();, 返回-1476508766,是否可以在sql server中

我正在尝试获取SQL Server 2008中guid.getHashCode()的等效int值。我尝试了校验和(uniqueidentifier var),但它没有返回相同的值。是否可以在SQL Server中获取guid的哈希代码值

比如说

Guid guid = new Guid("A0AE0446-3C50-479A-A909-3BA9C711007E"); 
int hash = guid.GetHashCode();, 
返回-1476508766,是否可以在sql server中获得相同的值

SELECT HASHBYTES('MD5',CAST(variable AS VARBINARY(MAX)))

要执行此操作,您需要知道
Guid.GetHashCode()
如何计算值。使用DotPeek:

public override int GetHashCode()
{
  return this._a ^ ((int) this._b << 16 | (int) (ushort) this._c) ^ ((int) this._f << 24 | (int) this._k);
}
作为T-SQL函数实现:

create function dbo.GetDotNetHashCode(@guid uniqueidentifier)
returns int
as
begin

    -- calculates a hash code value equivalent to .NETs Guid.GetHashCode() method

    declare @bytes binary(16) = cast(@guid as binary(16)),
            @hashCode int

    -- when converting to binary, the byte order of the first 3 segments is reversed

    declare @_a int = convert(int, 
                            substring(@bytes, 4, 1) + 
                            substring(@bytes, 3, 1) + 
                            substring(@bytes, 2, 1) + 
                            substring(@bytes, 1, 1))

    declare @_b int = convert(smallint, 
                            substring(@bytes, 6, 1) + 
                            substring(@bytes, 5, 1))

    declare @_c int = convert(int, 
                            substring(@bytes, 8, 1) + 
                            substring(@bytes, 7, 1))

    declare @_f int = convert(int, substring(@bytes, 11, 1))
    declare @_k int = convert(int, substring(@bytes, 16, 1))

    -- shift @_b using a bigint to avoid overflow
    -- left shift 16 is equivalent to multiplying by 2^16

    declare @_b_shift bigint = cast(@_b as bigint) * 65536
    set @_b = convert(int, substring(cast(@_b_shift as varbinary(8)), 5, 4))

    -- shift @_f using a bigin to avoid overflow
    -- left shift 24 is equivalent to multiplying by 2^24

    declare @_f_shift bigint = cast(@_f as bigint) * 16777216
    set @_f = convert(int, substring(cast(@_f_shift as varbinary(8)), 5, 4))

    set @hashCode = @_a ^ (@_b | @_c) ^ (@_f | @_k)

    return @hashCode

end
并使用以下内容进行测试(注释具有调用
Guid.GetHashCode()
的预期值):


注意:这是在SQL Server 2012中测试的,但我相信它在2008年也能工作。

该脚本返回的int值与guid.getHashCode()不一样。。例如,如果我的Guid=newGUID(“A0AE0446-3C50-479A-A909-3BA9C711007E”);int hash=guid.GetHashCode();,返回-1476508766,是否可以在sql server中获得相同的值?谢谢!这个解决方案非常有效。不幸的是,我不能投票给这个答案,因为我还没有15个声誉。@JeffOgata,你知道如何将这个扩展到散列一系列唯一标识符吗?((17*23+guid1)*23+guid2)*23+guid3等等,但这将溢出,我认为不可能在tsql中封装算术运算
create function dbo.GetDotNetHashCode(@guid uniqueidentifier)
returns int
as
begin

    -- calculates a hash code value equivalent to .NETs Guid.GetHashCode() method

    declare @bytes binary(16) = cast(@guid as binary(16)),
            @hashCode int

    -- when converting to binary, the byte order of the first 3 segments is reversed

    declare @_a int = convert(int, 
                            substring(@bytes, 4, 1) + 
                            substring(@bytes, 3, 1) + 
                            substring(@bytes, 2, 1) + 
                            substring(@bytes, 1, 1))

    declare @_b int = convert(smallint, 
                            substring(@bytes, 6, 1) + 
                            substring(@bytes, 5, 1))

    declare @_c int = convert(int, 
                            substring(@bytes, 8, 1) + 
                            substring(@bytes, 7, 1))

    declare @_f int = convert(int, substring(@bytes, 11, 1))
    declare @_k int = convert(int, substring(@bytes, 16, 1))

    -- shift @_b using a bigint to avoid overflow
    -- left shift 16 is equivalent to multiplying by 2^16

    declare @_b_shift bigint = cast(@_b as bigint) * 65536
    set @_b = convert(int, substring(cast(@_b_shift as varbinary(8)), 5, 4))

    -- shift @_f using a bigin to avoid overflow
    -- left shift 24 is equivalent to multiplying by 2^24

    declare @_f_shift bigint = cast(@_f as bigint) * 16777216
    set @_f = convert(int, substring(cast(@_f_shift as varbinary(8)), 5, 4))

    set @hashCode = @_a ^ (@_b | @_c) ^ (@_f | @_k)

    return @hashCode

end
/*

aeee939b-d2c8-4411-9cf9-94ab520e3c1c : -400107626
88ed5159-7900-4530-9886-841b7d42665b : 1978471474
2910ba35-ab2f-42a5-aed1-ea710d25a42e : 1749022910
0430b8b0-981b-4f1a-82fb-afe0725e8fd3 :  858519417
ba8cd9e4-fa1d-4ead-9145-b830aea6a641 : -124676344
9d5b5203-a2d4-4b8e-86dd-7eb5e4250960 : 1099897325
28b88e48-acc4-4f74-be28-4fcb92ad8c55 : -881016471
892056e9-dde1-4999-92ba-6c74b4a583c2 :  952180658
f0c3d3b5-bffc-4bda-b45a-70825f33b01f : 1061132400
df218e55-d672-48aa-b105-47578869068d : 1314113138

*/

declare @test table (Guid uniqueidentifier)

insert @test values
    ('aeee939b-d2c8-4411-9cf9-94ab520e3c1c'),
    ('88ed5159-7900-4530-9886-841b7d42665b'),
    ('2910ba35-ab2f-42a5-aed1-ea710d25a42e'),
    ('0430b8b0-981b-4f1a-82fb-afe0725e8fd3'),
    ('ba8cd9e4-fa1d-4ead-9145-b830aea6a641'),
    ('9d5b5203-a2d4-4b8e-86dd-7eb5e4250960'),
    ('28b88e48-acc4-4f74-be28-4fcb92ad8c55'),
    ('892056e9-dde1-4999-92ba-6c74b4a583c2'),
    ('f0c3d3b5-bffc-4bda-b45a-70825f33b01f'),
    ('df218e55-d672-48aa-b105-47578869068d')

select Guid, dbo.GetDotNetHashCode(Guid) as HashCode from @test