Felix D.
Felix D.

Reputation: 5093

Understanding this delphi method and converting it to C#

Could anyone explain to me what this delphi code is doing ?

I know it is shifting some bits but I don't get the purpose ..

function PKV_GetKeyByte(const Seed : Int64; a, b, c : Byte) : Byte;
    begin
        a := a mod 25;
        b := b mod 3;
        if a mod 2 = 0 then
            result := ((Seed shr a) and $000000FF) xor ((Seed shr b) or c)
        else
            result := ((Seed shr a) and $000000FF) xor ((Seed shr b) and c);
end;

How would I translate that to C# ?

This is what I got so far:

private byte PKV_GetKeyByte(Int64 seed, byte a, byte b, byte c)
{
    byte result;
    int aVal = a % 25;
    int bVal = b % 3;

    //IF-IS-MISSING
    if(a % 2 == 0)
    {

    }
    else
    {

    }            
    return result;
}

Upvotes: 1

Views: 237

Answers (1)

BugFinder
BugFinder

Reputation: 17858

I believe (untested) you want something like...

result := ((Seed shr a) and $000000FF) xor ((Seed shr b) or c))

goes to

result = System.Convert.ToByte((Seed >> a) & 0x000000FF) ^ ((Seed >> b) | c));

and

result := ((Seed shr a) and $000000FF) xor ((Seed shr b) and c));

goes to

result = System.Convert.ToByte((Seed >> a) & 0x000000FF) ^ ((Seed >> b) & c));

Upvotes: 5

Related Questions