hello, I want to change n bytes of a binary file

could ildg could.net at gmail.com
Tue Nov 1 20:31:40 EST 2005


Thanks to Fredrik Lundh,
It works~

On 11/2/05, Fredrik Lundh <fredrik at pythonware.com> wrote:
>
> "could ildg" wrote:
>
> > > so how can I do the binary stuff?
> >
> > 8-bit strings contain bytes.
> >
> > > I want a encrypt function like below:
> > > def encrypt(filename,n):
> >
> > f = open(filename,"rb+")
> >
> > > a=f.read(n)
> >
> > b = encrypt(a)
>
> ^^^^^^^^Thank you~~,but where is the encrypt defined?
>
> oh, I though you asked how to update the first n bytes of a
> binary file.
>
> if you want to XOR the bytes, you can do something like:
>
> import array
> b = array.array("B", a)
> for i in range(len(b)):
> b[i] = b[i] ^ 255
> b = b.tostring()
>
> or
>
> b = "".join([chr(ord(c) ^ 255) for c in a])
>
> or some variation thereof.
>
> </F>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051102/72aa7508/attachment.html>


More information about the Python-list mailing list