ashish
ashish

Reputation: 319

C++ design : Design to support Unicode msg as well as ASCII msg

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:

Upvotes: 0

Views: 80

Answers (1)

Andrew Komiagin
Andrew Komiagin

Reputation: 6556

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);
}

Upvotes: 1

Related Questions