How to convert unicode string to unsigned char *

Jean-Paul Calderone exarkun at divmod.com
Mon May 5 09:48:36 EDT 2008


On Mon, 5 May 2008 15:41:08 +0200, Simon Posnjak <sposnjak at gmail.com> wrote:
>Hi!
>
>I have a C module for which I created a wrapper with swig. The function def is:
>
>C:
>
>int some_thing(unsigned char * the_str);
>
>eg:
>
>Python:
>
>some_module.some_thing (the_str)
>
>Now I would like to feed it with a UTF-8 formatted string:
>
>test = u'Make \u0633\u0644\u0627\u0645, not war.'

`test´ is not a UTF-8 encoded string.  It's a unicode string.

To get a UTF-8 encoded string from a unicode string, use the `encode´
method:

    some_module.some_thing(test.encode('utf-8'))

Jean-Paul



More information about the Python-list mailing list