473,725 Members | 2,244 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 5451
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
24530
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
6215
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
6577
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
34594
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
21444
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
26625
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
7532
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
13511
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
9393
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9248
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...
1
9164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9092
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
8072
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...
1
6695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2151
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.