If your task is to "translate" code from C to Python line by line @th33lf's answer is perfectly fine.
However, you can use built-in features of Python which allows you to implement same logic in one-liner (suggested in this comment):
13 ^ reduce(xor, msg) | 0x40
reduce() iterates over iterable and applies provided function (we used xor()) to every item using result of previous step as first argument.
So pythonic version of your C function is:
from functools import reduce
from operator import xor
def asap_xor(msg):
return 13 ^ reduce(xor, msg) | 0x40
Note that msg should be an iterable of int values (already said in this comment). If your input value is string you can use str.encode() to convert str to bytes.
If you need to keep len argument (as you've said in this comment) you can pass sliced msg to reduce() (check this comment). I'd set default length argument value to 0 and use islice() only if some non-zero length passed to function.
from functools import reduce
from operator import xor
from itertools import islice
def asap_xor(msg, length=0):
return 13 ^ reduce(xor, islice(msg, length) if length else msg) | 0x40
Usage:
asap_xor("message".encode()) # 102
asap_xor("message".encode(), 6) # 67
asap_xor(b"message") # 102
asap_xor(b"message", 6) # 67
asap_xor([109, 101, 115, 115, 97, 103, 101]) # 102
asap_xor([109, 101, 115, 115, 97, 103, 101], 6) # 67
msgis abytesobject you can use13 ^ reduce(xor, msg) | 0x40. Imports:reduce(),xor(). Example.bytesobject, any iterable ofintvalues will fit. Example.125, you pass124which exclude last symbol, that's why results are different. If that's expected behavior, you can implement it using simple slice notation (reduce(xor, msg[:length])) or withislice()(reduce(xor, islice(msg, length))). Updated function.