【问题标题】:Concat a wchar_t and TCHAR连接 wchar_t 和 TCHAR
【发布时间】:2011-12-21 17:57:40
【问题描述】:

您好,我需要在我的项目中连接我的对话框名称(类型 wchar_t)和配置名称(类型 TCHAR)。 我怎样才能做到这一点? 谢谢。

【问题讨论】:

    标签: c++ visual-c++


    【解决方案1】:

    这取决于,TCHAR 是 charwchar_t,这取决于您是否将应用程序构建为 Unicode。如果您将应用程序构建为 Unicode,您可以简单地执行以下操作:

    wcscat_s(dest, extra);
    

    如果您不将应用程序构建为 Unicode,则需要将 TCHAR:s 字符串(即 char:s 字符串)转换为 wchar_t:s 字符串或wchar_t:s 转换成char:s 的字符串。为此,您应该查看 MultiByteToWideCharWideCharToMultiByte 函数。这两个参数看起来有点吓人,所以我通常使用一些帮助程序(请注意,为了清楚起见,适当的错误处理已被删除,一个适当的解决方案也会在一个调整大小的循环中调用上述函数如果调用失败并返回 ERROR_INSUFFICIENT_BUFFER):

    std::wstring multiByteToWideChar(const std::string &s)
    {
      std::vector<wchar_t> buf(s.length() * 2);
      MultiByteToWideChar(CP_ACP,
                          MB_PRECOMPOSED,
                          s.c_str(),
                          s.length(),
                          &buf[0],
                          buf.size());
      return std::wstring(&buf[0]);
    }
    
    std::string wideCharToMultiByte(const std::wstring &s)
    {
      std::vector<char> buf(s.length() * 2);
      BOOL usedDefault = FALSE;
      WideCharToMultiByte(CP_ACP,
                          WC_COMPOSITECHECK | WC_DEFAULTCHAR,
                          s.c_str(),
                          s.length(),
                          &buf[0],
                          buf.size(),
                          "?",
                          &usedDefault);
      return std::string(&buf[0]);
    }
    

    除此之外,我还设置了一个类型特征类,这样我就可以将我的项目编译为 Unicode 或不考虑:

    template <class CharT>
    struct string_converter_t;
    
    template <>
    struct string_converter_t<char>
    {
      static std::wstring toUnicode(const std::string &s)
      {
        return multiByteToWideChar(s);
      }
      static std::string toAscii(const std::string &s)
      {
        return s;
      }
      static std::string fromUnicode(const std::wstring &s)
      {
        return wideCharToMultiByte(s);
      }
      static std::string fromAscii(const std::string &s)
      {
        return s;
      }
    };
    

    还有一个几乎相同的 wchar_t 实例(我将其留作练习)。在你的情况下,你可以简单地做:

    std::wstring result = dialog_name + string_converter_t<TCHAR>::toUnicode(config_name);
    

    【讨论】:

    • 当然,过去十年构建的任何应用程序都可能构建为 Unicode,因此这是一项非常简单的任务。 :-)
    • 谢谢,我使用了 wcscat_s(dest, extra);它工作 TCHAR 是一个 wchar,我用 unicode 构建我的项目 :)
    • @CodyGray:是的,但是如果您想确保它始终有效,仅仅将您的代码设置为 UTF-16 是不够的。 UTF-16 中的一个代码点实际上可以跨越多个wchar_t,这是很少有人考虑的(包括我在内)。
    【解决方案2】:

    您的意思是 TCHAR*?因为将单个字符作为名称会有点奇怪。无论如何:只需将 TCHAR 转换为 wchar_t - TCHAR 是 char 或 wchar_t,无论哪种方式都可以保存转换为 wchar_t。

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

    【讨论】:

      【解决方案3】:

      这个问题类似这样:Cannot convert from 'const wchar_t *' to '_TCHAR *'

      要手动操作不同类型的字符,请查看:http://www.codeproject.com/KB/TipsnTricks/StringManipulations.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 2014-02-11
        • 1970-01-01
        • 2019-10-13
        • 2019-06-16
        • 1970-01-01
        相关资源
        最近更新 更多