String: How to switch between ANSI and Unicode

zhaozj2021-02-16  53

String: How to switch between ANSI and Unicode

Question: How do I switch between ANSI and Unicode?

answer:

This answer inspired a reply from Yves M (Reply)

ANSI to Unicode:

This conversion function multibytetowideChar () is completed

Code: ------------------------------------------------ -------------------------------- CHAR * ANSISTR = "Hello"; int a = lstrlena (ANSISTR) 1; BStr Unicodestr = SysallocStringlen (NULL, 2 * a); MultibyTowideChar (CP_ACP, 0, ANSISTR, A, Unicodestr, A); AFXMESSAGEBOX (CSTRING (Unicodestr), MB_OK, 0); // Displays "Hello" // ... When Done, Free the BSTR Sysfreestring (Unicodestr); --------------------------------------- -----------------------------------------

Unicode to ANSI:

In most cases, most of the OLE functions are returned, like this

Code: ------------------------------------------------ -------------------------------- HRESULT Someolefunction (bstr & bstr) {bstr = :: sysallocstring (l "hello") Return S_OK;} --------------------------------------------- -----------------------------------

This conversion is done with the widechartomultibyte () function.

Code: ------------------------------------------------ ------------------------------- BSTR UNICODESTR; Someolefunction (unicodestr); int a = sysStringlen (UnicodeStr) 1; Char * ANSISTR = New Char [A]; WideChartomultibyte (CP_ACP, 0, Unicodestr, -1, Ansistr, A, Null, NULL); AFXMessageBox (Ansistr, MB_OK, 0); // Will Display "Hello" // .. . rsuse: delete [] ANSISTR; SYSFREESTRING (UNICODESTR); ---------------------------------------------------------------------------------------------------- --------------------------------------------------

转载请注明原文地址:https://www.9cbs.com/read-24459.html

New Post(0)