[Tutor] Converting ascii string to hex?
Sean 'Shaleh' Perry
shalehperry at comcast.net
Tue Aug 12 23:39:45 EDT 2003
On Tuesday 12 August 2003 21:57, Terry Carroll wrote:
>
> There *is* a cleaner way, isn't there? Is there nothing akin to what I
> imagine would be simply
>
> hex(s)
>
> where "s" is a string?
>
> The above feels like I'm writing in Perl again.
def asctohex(s):
empty = '' # I use this construct because I find ''.join() too dense
return empty.join(['%02x' % ord(c) for c in s]) # the %02 pads when needed
or for those eschewing list comps:
return empty.join(map(lambda c: '%02x' % ord(c), s))
More information about the Tutor
mailing list