SqlServer Checksum in C#

前端 未结 5 672
忘了有多久
忘了有多久 2020-12-06 10:55

I\'m using the chechsum function in sql server 2008 R2 and I would like to get the same int values in a C# app. Is there any equivalent method in c# that returns the values

5条回答
  •  盖世英雄少女心
    2020-12-06 11:52

    @Dan's implementation of BinaryChecksum can be greatly simplified down in c# down to

    int SqlBinaryChecksum(string text)
    {
        uint accumulator = 0;
        for (int i = 0; i < text.Length; i++)
        {
            var leftRotate4bit = (accumulator << 4) | (accumulator >> -4);
            accumulator = leftRotate4bit ^ text[i];
        }
        return (int)accumulator;
    }
    

    This also makes it clearer what the algorithm is doing. For each character, a 4 bit circular shift then an xor with character's byte

提交回复
热议问题