[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: md5 hash problems
From:       "Chris Rebert" <clp () rebertia ! com>
Date:       2008-09-30 21:49:38
Message-ID: 47c890dc0809301449k30e8a1t165d2b7356109b9d () mail ! gmail ! com
[Download RAW message or body]

On Tue, Sep 30, 2008 at 2:25 PM, Michele <michele@nectarine.it> wrote:
> Hi there,
> why is this code generating a problem?
>
>>>> input = open('foo.img','rb').read().decode('ISO-8859-1')
>>>> import md5
>>>> md5.new(input).hexdigest()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xdc' in
> position 6:
> ordinal not in range(128)

You're trying to run md5 over a unicode string, but I'm pretty sure
it's only defined for bytes, and when Python tries to autoconvert the
unicode to bytes (by encoding it in ASCII), this fails because you
particular unicode string contains non-ASCII characters. Basically
make sure to pass md5.new() bytes and not unicode, either by removing
the call to .decode(), or calling .encode() on the unicode object
before passing it to md5.new()

Regards,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>>>>
>
> Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic