Converting numbers to unicode charaters

Paul Hankin paul.hankin at gmail.com
Mon Sep 24 10:23:55 EDT 2007


On Sep 24, 2:42 pm, byte8b... at gmail.com wrote:
> Here's how I'm doing this right now, It's a bit slow. I've just got
> the code working. I was wondering if there is a more efficient way of
> doing this... simple example from interactive Python:
>
> >>> word = ''
> >>> hexs = ['42', '72', '61', '64']
> >>> for h in hexs:
>
> ...   char = unichr(int(h, 16))
> ...   word += char
> ...   print char
> ...
> B
> r
> a
> d>>> print word
>
> Brad
>
> Each hex_number is two digits. unichr converts that to a character
> that I append to previous ints that have been converted to chars. In
> this way, I convert a string of hex numbers to ints to letters, to
> words.

The cleanest code is:

word = ''.join(unichr(int(h, 16)) for h in hexs)

If you want fast, you can build a cache once that maps hex strings to
unicode characters something like this:

cache = dict((hex(i)[2:], unichr(i)) for i in range(256))

Then use something like your code:
word = ''
for h in hexs:
  word += cache[h]

Or a list comprehension:
word = ''.join([cache[h] for h in hexs])

Or a generator:
word = ''.join(cache[h] for h in hexs)

--
Paul Hankin




More information about the Python-list mailing list