• 字符串转化


    Data Conversion

    http://msdn.microsoft.com/en-us/library/0heszx3w.aspx

    Convert a string to integer

    int atoi(
    const char *str
    );
    int _wtoi(
    const wchar_t *str
    );

    Each function returns the int value produced by interpreting the input characters as a number. The return value is 0 for atoi and _wtoi, if the input cannot be converted to a value of that type.

      Required header

      Header file: stdlib.h

    ATL and MFC String Conversion Macros

     1 //Example 1
    2 // Convert LPCWSTR to LPCSTR.
    3 void ExampleFunction1(LPCWSTR pszW)
    4 {
    5 // Create an instance of CW2A, called pszA,
    6 // and initialize it with pszW.
    7 CW2A pszA(pszW);
    8 // pszA works like an LPCSTR, and can be used thus:
    9 ExampleFunctionA(pszA);
    10 // Note: pszA will become invalid when it goes out of scope.
    11 }
    12
    13 // Example 2
    14 // Use a temporary instance of CW2A.
    15 void ExampleFunction2(LPCWSTR pszW)
    16 {
    17 // Create a temporary instance of CW2A,
    18 // and initialize it with pszW.
    19 ExampleFunctionA(CW2A(pszW));
    20 // Note: the temporary instance becomes invalid
    21 // after the execution of the statement above.
    22 }
    23
    24 // Example 3
    25 // Incorrect use of conversion macros.
    26 void ExampleFunction3(LPCWSTR pszW)
    27 {
    28 // Create a temporary instance of CW2A,
    29 // save a pointer to it and then delete
    30 // the temportary instance.
    31 LPCSTR pszA = CW2A(pszW);
    32 // The pszA in the following line is an invalid pointer,
    33 // as the instance of CW2A has gone out of scope.
    34 ExampleFunctionA(pszA);
    35 }

    The recommended way of converting to and from BSTR strings is to use the CComBSTR class. To convert to a BSTR, pass the existing string to the constructor of CComBSTR. To convert from a BSTR, use COLE2[C]DestinationType[EX], such as COLE2T

      Requirements

      Header file: AtlBase.h, AtlConv.h (declared in AtlConv.h)

    http://msdn.microsoft.com/en-us/library/87zae4a3(v=VS.100).aspx

    Programming with CComBSTR

    1 // Declare a CComBSTR object. Although the argument is ANSI,
    2 // the constructor converts it into UNICODE.
    3 CComBSTR bstrMyString("Hello World");
    4 // Convert the string into an ANSI string
    5 CW2A szMyString(bstrMyString);
    6 // Display the ANSI string
    7 MessageBoxA(NULL, szMyString, "String Test", MB_OK);

    If you are using a string literal to modify a CComBSTR object, use wide character strings. This will reduce unnecessary conversions.

    As the CComBSTR class allocates a buffer to perform certain operations, such as the += operator or Append method, it is not recommended that you perform string manipulation inside a tight loop. In these situations, CStringT provides better performance.

      Requirements

      Header: atlbase.h

    http://msdn.microsoft.com/en-us/library/bdyd6xz6.aspx

     

    MultiByteToWideChar function

    int MultiByteToWideChar(
    __in UINT CodePage,
    __in DWORD dwFlags,
    __in LPCSTR lpMultiByteStr,
    __in int cbMultiByte,
    __out LPWSTR lpWideCharStr,
    __in int cchWideChar
    );

    例:

     1 char sText[20] = "多字节字符串!OK!";
    2 DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, sText, -1, NULL, 0);
    3 wchar_t *pwText = NULL;
    4 pwText = new wchar_t[dwNum];
    5 memset(pwText, 0, (dwNum+1) * sizeof(wchar_t));
    6 MultiByteToWideChar(CP_ACP, 0, sText, -1, pwText, dwNum);
    7 if(!pwText)
    8 {
    9 delete []pwText;
    10 pwText = NULL;
    11 }

      Requirements

      Header: Winnls.h (include Windows.h)

    http://msdn.microsoft.com/en-us/library/dd319072(v=VS.85).aspx

    WideCharToMultiByte function

    int WideCharToMultiByte(
    __in UINT CodePage,
    __in DWORD dwFlags,
    __in LPCWSTR lpWideCharStr,
    __in int cchWideChar,
    __out LPSTR lpMultiByteStr,
    __in int cbMultiByte,
    __in LPCSTR lpDefaultChar,
    __out LPBOOL lpUsedDefaultChar
    );

    例:

     1 wchar_t wText[20] = {L"宽字符转换实例!OK!"};
    2 DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);
    3 char *psText = NULL;
    4 psText = new char[dwNum];
    5 memset(psText, 0, (dwNum+1) * sizeof(char));
    6 WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);
    7 if(!psText)
    8 {
    9 delete []psText;
    10 psText = NULL;
    11 }

      Requirements

      Header: Winnls.h (include Windows.h)

    http://msdn.microsoft.com/en-us/library/dd374130.aspx

  • 相关阅读:
    马拉车算法【Manachar】
    AcWing算法进阶课 基础算法 启发式合并
    iframe嵌套跨域子页面变化高度自适应
    js数组中的每一项异步请求
    利用geo3d地图数据画地图上面的柱子
    地图上面加柱状图组
    antd-select选项位置偏移问题解决
    git
    React
    前端面试题
  • 原文地址:https://www.cnblogs.com/plus/p/2236840.html
Copyright © 2020-2023  润新知