Thanks to Fredrik Lundh,<br>It works~<br><span class="gmail_quote"></span><br><div><span class="gmail_quote">On 11/2/05, <b class="gmail_sendername">Fredrik Lundh</b> <<a href="mailto:fredrik@pythonware.com">fredrik@pythonware.com
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">"could ildg" wrote:<br><br>> > so how can I do the binary stuff?
<br>><br>> 8-bit strings contain bytes.<br>><br>> > I want a encrypt function like below:<br>> > def encrypt(filename,n):<br>><br>> f = open(filename,"rb+")<br>><br>> > a=f.read
(n)<br>><br>> b = encrypt(a)<br><br>^^^^^^^^Thank you~~,but where is the encrypt defined?<br><br>oh, I though you asked how to update the first n bytes of a<br>binary file.<br><br>if you want to XOR the bytes, you can do something like:
<br><br>    import array<br>    b = array.array("B", a)<br>    for i in range(len(b)):<br>        b[i] = b[i] ^ 255<br>    b = b.tostring()<br><br>or<br><br>    b = "".join([chr(ord(c) ^ 255) for c in a])
<br><br>or some variation thereof.<br><br></F><br><br><br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>