How to convert unicode string to unsigned char *
Simon Posnjak
sposnjak at gmail.com
Mon May 5 10:30:37 EDT 2008
On Mon, May 5, 2008 at 4:16 PM, Jean-Paul Calderone <exarkun at divmod.com> wrote:
>
> On Mon, 5 May 2008 16:05:08 +0200, Simon Posnjak <sposnjak at gmail.com> wrote:
>
> > On Mon, May 5, 2008 at 3:48 PM, Jean-Paul Calderone <exarkun at divmod.com>
> wrote:
> >
> > > 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'))
> > >
> >
> > 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.
some_module.some_thing(the_string) function is a swig generated
function from a C lib. The C lib function expects unsigned char *.
The generated function is:
SWIGINTERN PyObject *_wrap_some_thing(PyObject *SWIGUNUSEDPARM(self),
PyObject *args) {
PyObject *resultobj = 0;
unsigned char *arg1 = (unsigned char *) 0 ;
unsigned char result;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject * obj0 = 0 ;
if (!PyArg_ParseTuple(args,(char
*)"O:cpot_printer_simple_printf",&obj0)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_unsigned_char, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '"
some_thing"" "', argument " "1"" of type '" "unsigned char *""'");
}
arg1 = (unsigned char *)(argp1);
result = (unsigned char)some_thing(arg1);
resultobj = SWIG_From_unsigned_SS_char((unsigned char)(result));
return resultobj;
fail:
return NULL;
}
More information about the Python-list
mailing list