How to encode a long string of hex values in unicode easily in python

311 views Asked by At

I have hex code point values for a long string. For a short one, following is fine.

msg = unichr(0x062A) + unichr(0x0627) + unichr(0x0628)
print msg

However, since unichr's alternate api unicode() does exist, i thought there must be a way to pass an entire code point string to it. So far i wasn't able to do it.

Now i have to type in a string of 150 hex values (code points) like the 3 above to generate a complete string. I was hoping to get something like

msg = unicode('0x062A, 0x0627....')

I have to use 'msg' latter. Printing it was a mere example. Any ideas?

3

There are 3 answers

0
AudioBubble On BEST ANSWER

Perhaps something like this:

", ".join(unichr(u) for u in (0x062A, 0x0627, 0x0628))

Result:

u'\u062a, \u0627, \u0628'

Edit: This uses str.join.

3
BrenBarn On

Hard to tell what you're asking for exactly. Are you looking for u'\u062A\u0627\u0628'? The \u escape lets you enter individual characters by code point in a unicode string.

1
mvillaress On

Following your example:

>>> c = u'\u062A\u0627\u0628'
>>> print c
تاب