Re: CString to const char*



"NickP" <a@xxxxx> wrote in message
news:eHvzYzi4GHA.4256@xxxxxxxxxxxxxxxxxxxxxxx

For some reason it will not convert to const char* automatically, if I
try manually I just end up with a screwed up ASCII string still containing
0
bytes from the UNICODE equivilent.
Remember that there are two versions of CString: CStringA and CStringW.
Which one is used depends on whether UNICODE is defined or not...

char *pBuffer = new char[iString.GetLength()];
pBuffer = (char*)iString.GetBuffer(sizeof(pBuffer));
GetBuffer() would return "const wchar_t*" (since UNICODE is defined) not
char*. Casting the result to char* won't convet the string from ASCII to
unicode. See below

MessageBoxA(NULL, pBuffer, "", 0);

Strange thing is my CString class is within the WTL namespace, is
there
more than one implementation of CString available?

What would be the best object to use for string handling, I thought
this
class was okay but have read sources on the net that say std::string is
better...
It's a matter of opinion. Personally I prefer using std::string because it's
more standard compatiable

Preferably I'd like to just use CString and convert it nicely...
Thanks
loads in advance!
IIRC, CString has no function to convert Unicode characters to ASCII ones.
However you can use mbtowc() or MultiByteToWideChar() to do the job

BTW, you can use TCHAR instead of char and use your normal functions
(MessageBox instead of MessageBoxA) and avoid all the hassle...
--
Abdo Haji-Ali
Programmer
In|Framez


.



Relevant Pages

  • Re: CFile::Read problem ???
    ... As far as the C compiler is concerned, ... you can pretty much always assign a char ... as ASCII and wchar_t as Unicode. ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: ANSI oder nicht ANSI?
    ... Wenn dabei aber der String von Unicode auf Ansi gewandelt wird, damit die Funktion ablaufen kann, dann kann diese Umwandlung eine unerwartete Auswirkung haben. ... Die Basisfunktionen arbeiten mit 1 bzw. 2 Byte großen "Elementen", ohne sich darum zu kümmern, ob diese Elemente nun Zeichen, Teile von Zeichen oder ganz andere Informationen enthalten. ... Dann ist es auch nicht generell möglich, 1 Zeichen in einer Variablen vom Typ Char abzulegen, weil manche Zeichen eben länger sein können als nur ein Element vom Datentyp Char. ...
    (de.comp.lang.delphi.misc)
  • Re: CreateFile pathname
    ... >Instead of passing a char[] as parameter 1, ... Microsoft uses arrays of WCHAR to store UNICODE characters, ... or how to convert a string into the write format. ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Retrieving text from CEdit box as char *?
    ... Windows CE uses Unicode so strings coming from the OS such as window text ... If you really need a string of char, you need to convert the CString to ... Alternatively, if you don't actually need the string in ASCII format, just ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: porting to 2005
    ... This little function which retrieves a string from an ini file gives all ... I think lots of your problems are caused by incorrect use of 'char' data ... and you will build a Unicode application. ... const CString & section, ...
    (microsoft.public.vc.mfc)