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

8
  • So what's wrong with the Python code you posted? Commented Mar 11, 2011 at 22:49
  • 1
    BTW, your return data is on the wrong indentation level. Commented Mar 11, 2011 at 22:50
  • @David: The OP already posted the error message, as a comment in the code. Commented Mar 11, 2011 at 22:50
  • 1
    But the important detail (which types are unsupported) is missing... Commented Mar 11, 2011 at 22:52
  • @delnan: It's actually pretty obvious if you notice that data is a string (as that's the intent of the C code). Commented Mar 11, 2011 at 22:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.