Re: How to convert unicode string to unsigned char *

Discussion in 'Python' started by Jean-Paul Calderone, May 5, 2008.

  1. On Mon, 5 May 2008 16:05:08 +0200, Simon Posnjak <> wrote:
    >On Mon, May 5, 2008 at 3:48 PM, Jean-Paul Calderone <> wrote:
    >> On Mon, 5 May 2008 15:41:08 +0200, Simon Posnjak <> 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'))

    >
    >Yes you are correct. It is unicode string. But still if I use encode I
    >get the same error:
    >
    >TypeError with message: in method 'some_thing', argument 1 of type
    >'unsigned char *'
    >
    >So I am looking for a way to "cast" unicode string to unsigned char *.
    >


    You need to provide some more information about `some_module.some_thing´.
    How is it implemented? What Python type does it expect? If it doesn't
    take a unicode string and it doesn't take a byte string, I don't know
    what kind of string it does take.

    Jean-Paul
     
    Jean-Paul Calderone, May 5, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Posnjak
    Replies:
    0
    Views:
    483
    Simon Posnjak
    May 5, 2008
  2. Jean-Paul Calderone

    Re: How to convert unicode string to unsigned char *

    Jean-Paul Calderone, May 5, 2008, in forum: Python
    Replies:
    0
    Views:
    508
    Jean-Paul Calderone
    May 5, 2008
  3. Simon Posnjak
    Replies:
    1
    Views:
    721
    Martin v. Löwis
    May 5, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    798
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    743
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page