Converting numbers to unicode charaters

B

byte8bits

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
dBrad


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.

Perhaps I'm doing it wrong... any tips?

Thanks,
Brad
 
L

Laurent Pointal

(e-mail address removed) a écrit :
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
dBrad


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.

Perhaps I'm doing it wrong... any tips?

You have to go by int conversion to get codes, and by unichr to get
corresponding unicode chars, so this seem the solution.

You may eventually use a more condensed expression and avoid n
concatenation of n chars using join, like this:
>>> u''.join(unichr(int(x,16)) for x in ['42','72','61','64'])
u'Brad'
 
P

Paul Hankin

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)
 
D

Duncan Booth

Laurent Pointal said:
You may eventually use a more condensed expression and avoid n
concatenation of n chars using join, like this:
u''.join(unichr(int(x,16)) for x in ['42','72','61','64'])
u'Brad'

Or even avoid the loop completely:
hexs = ['42', '72', '61', '64']
u'\\x'.join(['']+hexs).decode('string-escape')
'Brad'

(but for that to work you really do need to be sure that all the values are
2 characters).
 
P

Peter Otten

Duncan said:
Laurent Pointal said:
You may eventually use a more condensed expression and avoid n
concatenation of n chars using join, like this:
u''.join(unichr(int(x,16)) for x in ['42','72','61','64'])
u'Brad'

Or even avoid the loop completely:
hexs = ['42', '72', '61', '64']
u'\\x'.join(['']+hexs).decode('string-escape')
'Brad'

(but for that to work you really do need to be sure that all the values are
2 characters).
Or
"".join(['42', '72', '61', '74']).decode("hex").decode("latin1")
u'Brat'

(same caveat)

Peter
 
L

Larry Bates

How about:

ord=''.join([unichr(int(x,16)) for x in hexs])

I don't know if its faster, but I'll bet it is.

-Larry

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
dBrad


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.

Perhaps I'm doing it wrong... any tips?

Thanks,
Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top