2

I want to convert this C++ code to Python v2:

static unsigned char asConvCode[3] = {0xFC, 0xCF, 0xAB};

void asConv(char* str, int size)
{
    int i = 0;

    for (i = 0; n< size; n++)
    {
        str[i] ^= asConvCode[n % 3];
    }
}

tried to make like that:

def asConv(self, data):
    asConvCode= [0xFC, 0xCF, 0xAB]

    for i in range(len(data)):
        data[i] ^= asConvCode[i % 3] # Error: Unsupported operand type(s) for ^=: ...

        return data

I will be happy for any hint

Jonathan
  • 115
  • 3

1 Answers1

2

In Python, characters in strings are simply strings of length 1, not integers. So you must use this:

data[i] = chr(ord(data[i]) ^ asConvCode[i % 3])

Also, as I wrote in a comment, your return data is at the wrong indentation level, and will cause your function to return after processing the first character.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435