0

Currently my code have CString DisplayMessage, which is being used by my code all over to exchange DisplayMessage between clients and servers. Now as software is going international, I want DisplayMessage to have Unicode string message.

One technique I found is to

  1. Create:

    Class CDisplayMessage{ CString ASCIIMsg; CStringW UnicodeMsg; bool IsUnicode; ... }; 

    ASCII msg is required, So that I can make message backward compatible.

  2. Replace data type of CString DisplayMessage to CDisplayMessage DisplayMessage.

  3. And then need to change all the places where it is being used (that is more headache). Usage is like:

    DisplayMessage = some other CString; DisplayMessage = "sdfsdf"; 

Question:

Can anyone suggest me to provide some other solution or improve my solution, so that their is minimum change to do at all other places.

Note:

  • Platform : MS VISUAL STUDIO (Windows), C++
  • CharSet: Multi-Byte Character Set(can't change)

1 Answer 1

1

I would simply use CStringW and convert to CStringA if necessary. Please note that CString depends on _UNICODE settings. So it compiles to CStringW if UNICODE is defined and to CStringA in case of MBCS.

The conversion is real simple:

CStringW sTestW( L"Test" ); CStringA sTestA( "Test" ); // ASCII <-> UTF16 CStringW sConvertW = sTestA; CStringA sConvertA = sTestW; // UTF16 <-> UTF8 CStringA CUtility::UTF16toUTF8(const CStringW& utf16) { return CW2A(utf16, CP_UTF8); } CStringW CUtility::UTF8toUTF16(const CStringA& utf8) { return CA2W(utf8, CP_UTF8); } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank for the solution Andrew, But UNICODE macro is my restriction as I mentioned that my project is Multi-byte charset and I can not cange it to UNICODE charset. Secondly I want to use a variable that can take both kind of values ASCII as well as UNICODE, because some systems are still using ASCII. So I need to preserve both.
That's fine. You can use CStringA and CStringW anyways in MBCS. So basically the solution remains the same for both UNICODE and MBCS builds.
If I change to CStringW then It can not accept ASCII msg. I want to use a variable that can take both kind of values ASCII as well as UNICODE, because some systems are still using ASCII. So I need to preserve both.
sure you can simply assign your CStringW to ASCII string. it will convert it internally to UTF16. The UNICODE charset is much wider than ASCII. So it is totally safe. See my examples. Please provide real scenario (code snippet) to demonstrate how you going to use it. So far I don't see any problems sticking with just CStringW.
Yes, I think you are right. but Still I have one query. Suppose I have Unicoded msg in CStringW dispmsg . Now If I want to convert this Unicoded msg to ASCII. Is there any way to do this ? Assume that msg is of English alphabets only.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.