Re: How to convert wxString to unsigned char *

315 views
Skip to first unread message

iko...@earthlink.net

unread,
Nov 23, 2009, 10:40:32 PM11/23/09
to wx-u...@googlegroups.com
Domingo,

-----Original Message-----
>From: Domingo Becker <doming...@gmail.com>
>Sent: Nov 23, 2009 6:45 AM
>To: wx-u...@googlegroups.com
>Subject: Re: How to convert wxString to unsigned char *
>
>
>2009/11/22 <iko...@earthlink.net>:
>>
>> Hi, Eran,
>>
>> -----Original Message-----
>>>From: Eran Ifrah <eran....@gmail.com>
>>>Sent: Nov 22, 2009 12:08 AM
>>>To: wx-u...@googlegroups.com
>>>Subject: Re: How to convert wxString to unsigned char *
>>>
>>>
>>>You can try:
>>>string.mb_str(wxConvUTF8).data()
>>> ( you can use other conversions, but wxConvUTF8 will do the work for
>>>most cases)
>>>but it returns char* and not unsinged char*
>>>
>>>but it works for me
>>
>> No, it does not work for me.
>> I need an "unsigned char *" as I will cast it to "SQLCHAR *", which is, I think,
>> 7-bit ASCII.
>>
>> Any other ideas?
>>
>
>(const char *) thestring.mb_str()

I'm looking for an "unsigned char *" not the "const char *"...

All I'm looking for is wxString -> unsigned char */7-bit ASCII conversion.

Thank you.

>
>>

iko...@earthlink.net

unread,
Nov 24, 2009, 11:47:06 AM11/24/09
to wx-u...@googlegroups.com
Hi, ALL,
I tried to call ToAscii(), but got an error message:

"Can't convert wxCharBuffer to unsigned char *".

I'm on the trunk + MSVC + Windows XP.

Thank you.

Eran Ifrah

unread,
Nov 24, 2009, 11:59:12 AM11/24/09
to wx-u...@googlegroups.com
This is because you need to call:
str.ToAscii().data()

on Unicode build ToAscii() returns wxCharBuffer
--
Eran Ifrah
Cross platform, open source C++ IDE: http://www.codelite.org

iko...@earthlink.net

unread,
Nov 24, 2009, 1:46:41 PM11/24/09
to wx-u...@googlegroups.com
Hi, Eran,

-----Original Message-----
>From: Eran Ifrah <eran....@gmail.com>
>Sent: Nov 24, 2009 8:59 AM
>To: wx-u...@googlegroups.com
>Subject: Re: How to convert wxString to unsigned char *
>
>
>This is because you need to call:
>str.ToAscii().data()
>
>on Unicode build ToAscii() returns wxCharBuffer

I just tried:

unsigned char *test = (unsigned char *) str.ToAscii().data();

and to my surprise, test is empty and *test is NULL.

I guess I need to use good, old C-style strcpy()....

And it's both on Windows and Linux with MSVC and gcc.

Thank you.

Vadim Zeitlin

unread,
Nov 24, 2009, 1:51:02 PM11/24/09
to wx-u...@googlegroups.com
On Tue, 24 Nov 2009 10:46:41 -0800 (GMT-08:00) iko...@earthlink.net wrote:

> I just tried:
>
> unsigned char *test = (unsigned char *) str.ToAscii().data();
>
> and to my surprise, test is empty and *test is NULL.

I recommend reading about C++ temporaries and actually understanding what
is going on in the above line first. This will really be simpler than
asking the questions about the same problem again and again on the mailing
list during at least several months.

> I guess I need to use good, old C-style strcpy()....

Please don't, it's not without its pitfalls as well. Do everyone a favour
and use wxStrlcpy() instead at least.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Reply all
Reply to author
Forward
0 new messages