Mutual conversion between wide character and ANSI characters

zhaozj2021-02-16  41

At present, there are two ways: can provide conversion between widgets and ANSI characters.

The first function provided by the COM library

CHAR * _COM_UTIL :: ConvertBSTRTSTRING (BSTR);

BSTR _COM_UTIL :: ConvertStringTObstr (char *);

Example // ConvertBSTRTRING.CPP # include #include #pragma comment (lib, "comsupp.lib") int main () {bstr bstrtext = :: sysallocstring (l "test"); WPrintf (L "BSTR TEXT:% S / N", BSTRTEXT); char * lpsztext2 = _com_util :: converTRTRTRTSTRING (BSTRTEXT); Printf ("char * text:% s / n", lpsztext2); sysfreestring (bstrtext); delete LPSZTEXT2;

Outputbstr Text: Testchar * Text: Test

The function provided by the second standard library

Convert width string WCSTR to ANSI string MBSTR

SIZE_T WCSTOMBS (Char * MBSTR, Const Wchar_T * WCSTR, SIZE_T Count);

The address of the MBSTR Multi-Character's address WCSTR Wide Character is stored in the maximum number of bytes of multi-byte characters to convert the ANSI string MBSTR to a wide string WCSTR.

SIZE_T MBSTOWCS (Wchar_T * WCSTR, Const Char * Mbstr, Size_t Count); Parameters

WCSTR wide string of address MBSTR Multi-byte string (ANSI) address count of multibly character to convert EXAMPLE

/ * Mbstowcs.cpp illustrate the behavior of the mbstowcs function * /

#include #include

Void main (void) {INT i; char * pmbnull = null; char * pmbhello = (char *) malloc (MB_CUR_MAX); wchar_t * pwchello = l "hi"; wchar_t * pwc = (wchar_t *) Malloc (slhar_t ));

Printf ("Convert to Multibyte String: / N); i = WCSTombs (PMBHELLO, PWCHELLO, MB_CUR_MAX); Printf (" / Tcharacters Converted:% U / N ", I); Printf (" / THEX VALUE OF FIRST ") PRINTF ("Multibyte Character:% #. 4X / N / N", PMBHELLO);

Printf ("Convert Back to Wide-Character String: / N"); i = MBSTOWCS (PWC, PMBHELLO, MB_CUR_MAX); Printf ("/ Tcharacters Converted:% U / N", I); Printf ("/ THEX VALUE OF "); Printf (" Wide Character:% #. 4x / n / n ", PWC); delete [] PMBHELLO

DELETE [] PWC; // This example example is taken from MSDN, I think there is memory leak here, so I joined // The last two lines should be referred to herein, // ms-help: //ms.msdnqtr .2003feb.2052 / wcecrt / htm / _Wcecrt_MBstowcs.htm

}

OUTPUT

Convert to Multibyte String: Characters Converted: 1 HEX Value of First Multibyte Character: 0x0e1a

Convert Back to Wide-Character String: Characters Converted: 1 HEX Value of First Wide Character: 0x0e1e The function of the COM library needs to be transferred after the conversion, the release of the COM library is allocated by the COM library, the function provided by the standard library requires I have used a buffer area in advance to store and convert the string.

Welcome to discuss ...

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

New Post(0)