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
1 Answer 1
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.
answered Mar 11, 2011 at 22:52
C. K. Young
224k47 gold badges394 silver badges446 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
return datais on the wrong indentation level.datais a string (as that's the intent of the C code).