Converting strings to hex

emile at fenx.com emile at fenx.com
Thu May 18 22:24:41 EDT 2000


As long as we're contributing methods, I use this:

def hta(s, padlen=1):
    """ hta(string) returns a hex repr of string"""
    if not s:
        return ""
    elif type(s)<>type("string"):
        raise "argument should be of type string"
    elif len(s)<padlen:
        while len(s)<padlen:
            s = '\000' + s
    h = ""
    for c in s:
        h = h + '%2x' % ord(c)
    return h

Emile van Sebille
emile at fenx.com


> It must be something in the air. I also needed this yesterday and a
> DejaNews search yielded this function from Michael Wallace.
> 
> def hexify(binaryString):
>     result = ""
>     for i in binaryString:
>         result = result + string.zfill(hex(ord(i))[2:],2)
>     return result
> 
> Jay
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
>




More information about the Python-list mailing list