Problems with character, UniCode, CString, MFC - Convert from CString to Unicode and from Unicode to CString

Markus Freitag 3,786 Reputation points
2023-12-21T19:53:40.61+00:00

Hello,

if (component.Unit.Find("°C") != -1)
	 component.Unit.Replace("°C", "Grad Celsius");

I have problems with characters that I pass on via the TCP/IP interface. I think it is due to the character set. How can I solve this?

CString strNormal("Some °C Unicode string");
char ascstrNormal[32];
USES_CONVERSION;
strcpy_s(ascstrNormal, T2CA(strNormal));

CString str = _T("Some °C Unicode string");
char ascstr[32];
strcpy_s(ascstr, T2CA(str)); 

enter image description here

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,568 questions
{count} vote

Accepted answer
  1. WayneAKing 4,921 Reputation points
    2023-12-23T10:02:10.7066667+00:00

    Have you tried changing the encoding of the source file(s)?

    In the IDE, with a source file open and with focus, use the File menu and Save As.

    From the dialog that opens, the Save button has a dropdown list attached. Drop it and select Save with Encoding. Make note of the current encoding, then try a different one.

    In my tests "Western European (Windows) - Codepage 1252" and "Unicode UTF-8 with signature ..." do NOT show the extra character. But "Unicode UTF-8 without signature ..." DOES show that extra character.

    Rebuilds were done after each change to the encoding of the source file.

    • Wayne

1 additional answer

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2023-12-21T22:37:03.1766667+00:00

    >I have problems with characters that I pass on via the TCP/IP interface.

    >I think it is due to the character set.

    Which compiler and IDE are you using? Which versions?

    Which version of the C RTL via the Win SDK?

    FWIW when I build your example and display it using VS2017 Community Edition 15.9.58, Win SDK 10.0.17763.0, and ISO C++17 Standard (/std:c++17) I do NOT see that extra character.

    • Wayne

    1

    2

    3