How to convert CString to string without loosing any characters

sarath govind 1 Reputation point
2021-07-28T13:11:53.397+00:00

I have a MFC CString variable , I want to convert it to a string . Actually this CString variable holds a value which have many junk characters like this (!(¾�ÊO€N„+ E^]«��3àâ;bÛ) in between . I have tried possibilities like CT2CA , LPCTSTR etc. These conversion results to string having length not same as CString length . There is large difference between the sizes. Looking forward a method which does the correct conversion without loosing any characters

Developer technologies C++
{count} votes

1 answer

Sort by: Most helpful
  1. Guido Franzke 2,191 Reputation points
    2021-07-29T06:25:52.97+00:00

    Hello,
    I assume, you build in Unicode (because ANSI does not make sense in my opinion).
    So CString is a CStringW. std::string is always of type char. You should convert the CStringW to std::wstring. Read the documentation: basic_string

    If this is not the case: as others have already mentioned before, give us a short example what you want to do.

    Regards, Guido


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.