Getting hex value of a character

Grant Edwards grante at visi.com
Thu Dec 12 16:05:31 EST 2002


In article <Mq4K9.1393$vA1.74331385 at newssvr14.news.prodigy.com>, Dennis Reinhardt wrote:

>> ''.join(['%X' % ord(c) for c in 'AB'])
> Thanks.  Very readable.

I use a lot of strings that contain binary data.  If that's a
possibility you might want to specify a field-width and
zero-fill:

def strToHex(s):
  return ''.join(['%02x' % ord(c) for c in s])

Or, if you get tired of counting nibbles to find byte boundaries:

def strToHex(s):
  return ' '.join(['%02x' % ord(c) for c in s])

-- 
Grant Edwards                   grante             Yow!  A shapely CATHOLIC
                                  at               SCHOOLGIRL is FIDGETING
                               visi.com            inside my costume...



More information about the Python-list mailing list