代码之家  ›  专栏  ›  技术社区  ›  Cheok Yan Cheng

字符数组到LPCTSTR

c++
  •  0
  • Cheok Yan Cheng  · 技术社区  · 14 年前

    // el.strCap is char[50]
    // InsertItem is expecting TCHAR pointer (LPCTSTR)
    // How I can perform conversion?
    // I do not have access in both "list" and "el" source code
    // Hence, there is no way for me to modify their signature.
    list.InsertItem(i, el.strCap);
    

    不,我不想用

    WideCharToMultiByte
    

    它们太笨重了,不能使用。

    3 回复  |  直到 14 年前
        1
  •  3
  •   Dean Harding    14 年前

    various macros and helper classes 包括进行转换:

    char *test = "Hello World";
    CA2CT ct(test);
    list.InsertItem(i, ct);
    

    虽然说 WideCharToMultiByte 宽图表多字节 让它返回std::wstring或任何你需要的东西。事实上,基本上就是这样 CA2CT

        2
  •  1
  •   R Samuel Klatchko    14 年前

    如果您的字符串编码为ISO-8859-1,则很容易转换为UTF-16:

    // Convert an ISO-8859-1 string to a UTF-16 string
    wchar_t wstr[50];
    for (int i = 0; i < 50; ++i)
    {
        wstr[i] = el.strCap[i];
        if (!wstr[i])
            break;
    }
    

    MultiByteToWideChar

        3
  •  1
  •   humbagumba    14 年前

    你也可以用 CStringW list.InsertItem(i, CStringW("blah"));