473,701 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(unsi gned char * the_str);

eg:

Python:

some_module.som e_thing (the_str)

Now I would like to feed it with a UTF-8 formatted string:

test = u'Make \u0633\u0644\u0 627\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.som e_thing(test.en code('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.so me_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.som e_thing(the_str ing) 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_thi ng(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_ParseTu ple(args,(char
*)"O:cpot_print er_simple_print f",&obj0)) SWIG_fail;
res1 = SWIG_ConvertPtr (obj0, &argp1,SWIGTYPE _p_unsigned_cha r, 0 | 0 );
if (!SWIG_IsOK(res 1)) {
SWIG_exception_ fail(SWIG_ArgEr ror(res1), "in method '"
some_thing"" "', argument " "1"" of type '" "unsigned char *""'");
}
arg1 = (unsigned char *)(argp1);
result = (unsigned char)some_thing (arg1);
resultobj = SWIG_From_unsig ned_SS_char((un signed char)(result));
return resultobj;
fail:
return NULL;
}
Jun 27 '08 #1
1 5444
some_module.som e_thing(the_str ing) 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
24526
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 = source.GetBuffer(0 ); but i need a char* and so this is not working CString source = _T("TestString");
4
6212
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 how I can use them in my programs (writing and reading from file, from the console, processing Unicode strings and chars inside the program, etc...)? Thanx
5
6573
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. I want to make convert between Unicode to string and string to Unicode.
15
34591
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
21437
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. #include <string.h> #include <stdio.h> /* Convert an ipv4 address to long integer */ /* "192.168.1.1" --> 3232235777 */ unsigned long iptol(char *ip){
10
26618
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* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
2
7530
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 be read completely. and I cannot tell where its root located, so NEED your help, GIVE me a hand please. THX. static Status LoadXMLFile2String(const char *filename, wchar_t *text){ FILE *f; if(!(f = fopen(filename, "r"))){ __printDebugA("Input...
0
562
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 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
14
13507
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. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
8733
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8649
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9083
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3102
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2398
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.