how to turn arbitrary binary string into unix filename?

Shawn Dyer sdyer7073 at my-deja.com
Wed Feb 23 01:21:15 CET 2000


Here is another possible solution for that problem that I have used (it
takes advantage of the fact that a string is also a sequence type):
>>> import md5, string
>>> md=md5.md5()
>>> md.update('Test Message')
>>> md.digest()
'\321\324\030\013~A\034K\350k\000\373.\341\003\353'
>>> string.join(map(lambda x: '%02x' % ord(x), md.digest()),'')
'd1d4180b7e411c4be86b00fb2ee103eb'
>>>

In article <Pine.BSF.4.10.10002181615490.11934-100000 at chi.pair.com>,
  "Michal Wallace (sabren)" <sabren at manifestation.com> wrote:
> On Fri, 18 Feb 2000, Bill Janssen wrote:
>
> > I'm sort of surprised that there doesn't seem to be any standard way
> > to take a binary string in Python and turn it into a text string
> > suitable for a UNIX filename.  This is handy if you are trying to
map
> > data to filenames, for instance; you take the MD5 hash of the data,
> > for instance the Message-ID of a mail message, convert it to an
ASCII
> > string (typically by hexifying it), and use that as the filename.
But
> > after half an hour of poking around, I can't see a standard method
> > that one can call to hexify a string.
>
> Hey Bill,
>
> This code takes a binary string and converts it to a
> hexidecimal representation. (It doubles the length
> but only uses the characters 0-9 and a-f)
>
> def hexify(binaryString):
>     result = ""
>     for i in binaryString:
>         result = result + string.zfill(hex(ord(i))[2:],2)
>     return result
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list