简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM