[Tutor] how can I use unicode in ctypes?

Albert-Jan Roskam fomcl at yahoo.com
Thu Dec 6 17:04:40 CET 2012


Hi,
 
I am using ctypes to get and set data, among which, sometimes, unicode data. I was looking for a clean way  to encode and decode basestrings.
The code below illustrates the problem.
 
import ctypes

s = u'\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064a\u0643\u0645'
good = ctypes.c_char_p(s.encode("utf-8"))
bad = ctypes.c_char_p(s)
print good, bad 
# prints: c_char_p('\xd8\xa7\xd9\x84\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85 \xd8\xb9\xd9\x84\xd9\x8a\xd9\x83\xd9\x85') c_char_p('?????? ?????')

I find it ugly to encode and decode the strings everywhere in my code. Moreover, the strings are usually contained in dictionaries, which would make it even uglier/ more cluttered. 
So I wrote a @transcode decorator: http://pastecode.org/index.php/view/29608996 ... only to discover that brrrrrr, this is so complicated! (it works though).
Is there a simpler solution?

Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


More information about the Tutor mailing list