473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Converting numbers to unicode charaters

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.

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

Thanks,
Brad

Sep 24 '07 #1
5 1942
by*******@gmail.com 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
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.

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'

Sep 24 '07 #2
On Sep 24, 2:42 pm, byte8b...@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

Sep 24 '07 #3
Laurent Pointal <la*************@limsi.frwrote:
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).
Sep 24 '07 #4
Duncan Booth wrote:
Laurent Pointal <la*************@limsi.frwrote:
>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
Sep 24 '07 #5
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

by*******@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.

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

Thanks,
Brad
Sep 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: calfdog | last post by:
Hello, I have been doing some experimenting with Automating Internet Explorer for testing. I would like to read from Excel and use the data to set the values of my text boxes on my web forms....
1
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have create a populated a Access...
3
by: Supratim | last post by:
Hi, For past few weeks I am working on a function that would take encoded Unicode characters from query string of http requests and then decode them back to Unicode numbers. I have full success...
4
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have created a populated a Access...
3
by: Max Gattringer | last post by:
I have written a little programm, which converts normal Text into Unicode Bytes - nothing special, but i tried to creat 2nd Encoder which converts strings(numbers) in a textBox (strings which i...
7
by: MeganTSU | last post by:
Hey yall! I am trying to get this program finished for class.... It says that you are suppposed to write a program that will display a check formatted out put (the output looks like a check). I got...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
6
Robbie
by: Robbie | last post by:
Hi again all, here's something I'm stuck on... I'm making a function to convert a unicode character into the kind of code you need to put on a UTF-8 encoded web page (ampersand, hash, digits,...
5
by: John Ztwin | last post by:
Hello, I have a file that contains ordinary text and some special charaters in Unicode escape sequences (\uxxxx). When I read the file using e.g. StreamReader Unicode escape sequences are not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.