How do I convert XXX to a wxString and vice versa?
wxChar*
Just use wxString::c_str. For char*/wchar_t* see the UNICODE/ANSI section below (char* is a multibyte string and wchar_t is a unicode string - either 2 or 4 bytes to character depending on your compiler, prefixed with L"XXX" etc.).
Integers
int-->string
(Note that you can use wxString::Format in place of this, but the << operator is preferred for optimization reasons, and is easier)
string-->int
(A more official way to do a string-->int conversion is to use wxString::ToLong or wxString::ToULong)
Floats/Doubles
double-->string
(Note that you can use wxString::Format in place of this, but the << operator is preferred for optimization reasons, and is easier)
double-->int
(A more official way to do a string-->double conversion is to use wxString::ToDouble)
Pascal Strings (Mac usually)
Pascal Strings are a normal ansi string with a leading character that indicates their length and without a trailing null character.
In wx/mac/carbon/private.h (include wx/mac/private.h in your file) you can use the following to convert a wxString to a pascal string (you'll want to delete the pascal string afterwards
)
Code: Select all
void wxMacStringToPascal( const wxString&from , StringPtr to ) ;
wxString wxMacMakeStringFromPascal( ConstStringPtr from ) ;
Also, somewhat related is that you can get a FSSpec directly from a wxString:
Code: Select all
wxString wxMacFSRefToPath( const FSRef *fsRef , CFStringRef additionalPathComponent = NULL ) ;
OSStatus wxMacPathToFSRef( const wxString&path , FSRef *fsRef ) ;
wxString wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname ) ;
Basic String (BSTR in COM)
A COM AUTOMATION Basic String is a 2-byte-per-character (wxUint16*) string that is like a pascal string, only the leading data amount that contains its length is 4 bytes.
You can use the manual MSW calls SysAllocString etc., create the string yourself (not too difficult), or use wxBasicString from wx/msw/ole/oleutils.h.
Just do something like
UNICODE/ANSI
This is fairly involved. When converting UNICODE-->ANSI the encoding you pass is the encoding you want your DESTINATION string to be in - when converting from ANSI-->UNICODE, the encoding you pass in is the encoding of the multibyte SOURCE string.
Now, with that out of the way the general way to convert from UNICODE to ANSI and vice versa is to use the wxMBConv classes. These are pretty involved and are in wx/strconv.h, these classes contain the encoding within them - so there is no need to specify which encoding you want - just use the appropriate wxMBConv instance.
For better or worse, there are several -->GLOBAL<-- wxMBConv instances. Note that these are probably not thread-safe in any way!!!
wxConvLocal is the global wxMBConv instance that you'll want to use 99% of the time, its the encoding that wxWidgets uses. All of the examples here will use this.
wxConvLibc is the encoding the c library uses - rather than using optimized native calls it uses the mbtowc c library routines, which may not be implemented on your platform/compiler.
wxConvFile is the encoding the file system uses. There's A LOT of debate over this - essentially you're supposed to use it to wxFopen, but beyond that its quite involved. Try avoiding it if you can.
To convert from UNICODE or ANSI:
Code: Select all
//wx doesn't have a wxWXCharBuffer etc. yet so make our own
#ifdef UNICODE
#define wxMYCharBuffer wxWCharBuffer
#else
#define wxMYCharBuffer wxCharBuffer
#endif
//WX-->MultiByte
//for wxChar*
wxCharBuffer mbc = wxConvLocal.WX2MB(mywxcharptr);
//for wxString
wxCharBuffer mbc = mywxString.mb_str(wxConvLocal);
//MultiByte-->WX
wxMYCharBuffer buf = wxConvLocal.MB2WX(mycharptr);
//WX-->Wide Character
//for wxChar*
wxWCharBuffer wcc = wxConvLocal.WX2WC(mywxcharptr);
//for wxString
wxWCharBuffer wcc = mywxString.wc_str(wxConvLocal);
//Wide Character-->WX
wxMYCharBuffer buf = wxConvLocal.WC2WX(mywcharptr);
You're probably wondering about wc_str and mb_str. Well, these are better because they take into account embedded null characters in your string and don't have to call strlen.