how to turn arbitrary binary string into unix filename?

Michal Wallace (sabren) sabren at manifestation.com
Fri Feb 18 22:19:02 CET 2000


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



Cheers,

- Michal
-------------------------------------------------------------------------
http://www.manifestation.com/         http://www.linkwatcher.com/metalog/
-------------------------------------------------------------------------





More information about the Python-list mailing list