newbie question: any better way to write this code?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Dec 27 10:46:58 EST 2006


In <1167232702.061737.25610 at 42g2000cwt.googlegroups.com>, neoedmund wrote:

> i want to let a byte array to be xor with some value.
> but code show below i wrote seems not so .. good..., any better way to
> write such function? thanks.
>
> def xor(buf):
> 	bout=[]
> 	for i in range(len(buf)):
> 		x = ord(buf[i])
> 		x ^= 123
> 		bout.append(chr(x))
> 	return "".join(buf)
> buf = "xxxxxxxx".encode("utf8")
> buf = xor(buf)

def xor(buf):
    return ''.join(chr(ord(x) ^ 123) for x in buf)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list