user10147823
user10147823

Reputation:

How to fix 'Cannot implicitly convert type' error in C#?

I am doing the flare-on challenge (Memecat Battlestation) and I've decided to.. Well bypass it, the Demo Shareware one got bypassed, but corrupted flag so I thought I'd try the full version, I tried the full version but I've gotten an error

This is for the 1st Flare-On challenge named Memecat Battlestation, I am running Windows 7 64-bit, the error is in VictoryForm.cs

    private static IEnumerable<byte> AssignFelineDesignation(byte[] cat, IEnumerable<byte> data)
    {
        byte[] s = BattleCatManagerInstance.InvertCosmicConstants(cat);
        int i = 0;
        int j = 0;
        return data.Select(delegate (byte b)
        {
            i = (i + 1 & 255);
            j = (j + (int)s[i] & 255);
            BattleCatManagerInstance.CatFact(s, i, j);
            return b ^ s[(int)(s[i] + s[j] & byte.MaxValue)];
        });
    }

I've expected it to just build without any errors but I've gotten this error:

'Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<byte>'. An explicit conversion exists (are you missing a cast?)'

Upvotes: 2

Views: 666

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503859

This line:

return b ^ s[(int)(s[i] + s[j] & byte.MaxValue)]

... returns an int, because that's the type of the ^ operator you're using. You can just cast the result to byte:

return (byte) (b ^ s[(int)(s[i] + s[j] & byte.MaxValue)]);

I'd personally use a lambda expression rather than an anonymous method though:

return data.Select(b =>
{
    i = (i + 1 & 255);
    j = (j + (int)s[i] & 255);
    BattleCatManagerInstance.CatFact(s, i, j);
    return (byte) (b ^ s[(int)(s[i] + s[j] & byte.MaxValue)]);
});

Upvotes: 10

Related Questions