473,395 Members | 1,730 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,395 software developers and data experts.

Re: How to convert unicode string to unsigned char *

On Mon, May 5, 2008 at 4:16 PM, Jean-Paul Calderone <ex*****@divmod.comwrote:
>
On Mon, 5 May 2008 16:05:08 +0200, Simon Posnjak <sp******@gmail.comwrote:
On Mon, May 5, 2008 at 3:48 PM, Jean-Paul Calderone <ex*****@divmod.com>
wrote:
On Mon, 5 May 2008 15:41:08 +0200, Simon Posnjak <sp******@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;
}
Jun 27 '08 #1
1 5400
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:
If you don't want to change the generated function, I recommend to
put a wrapper around it, as Jean-Paul suggested:

def some_thing(s):
return some_thing_real(s.encode("utf-8"))

Regards,
Martin
Jun 27 '08 #2

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

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
4
by: Marco Iannaccone | last post by:
I'd like to start using Unicod (especially UTF-8) in my C programs, and would like some infos on how to start. Can you tell me some documents (possibily online) explaining Unidoce and UTF-8, and...
5
by: [Yosi] | last post by:
Hi, I have a string array includes unicode data, how can I print the char (real string), for example: "\x08\x03\x34\0.\0\x39\0" What should I do, I want to see the char of this array of unicode....
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
65
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
2
by: starffly | last post by:
I want to read a xml file in Unicode, UTF-8 or a native encoding into a wchar_t type string, so i write a routine as follows, however, sometimes a Unicode file including Chinese character cannot...
0
by: Jean-Paul Calderone | last post by:
On Mon, 5 May 2008 16:05:08 +0200, Simon Posnjak <sposnjak@gmail.comwrote: You need to provide some more information about `some_module.some_thing´. How is it implemented? What Python type does...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.