【问题标题】:Convert std::basic_string<Char> to string将 std::basic_string<Char> 转换为字符串
【发布时间】:2020-04-23 11:12:36
【问题描述】:

虽然 MediaInfoDLL 以std::basic_string&lt;Char&gt; 格式返回元数据(采样率、通道、流大小、标题...),但我需要将其转换为字符串以便以后处理。例如mi.Get(Stream_Audio, 0, __T("Performer"))std::basic_string&lt;Char&gt; 格式返回“艺术家姓名”。

你能帮帮我吗?

提前谢谢你

【问题讨论】:

  • 您的问题中Char 到底是什么?是否相当于内置类型char?如果是这样,我有一些好消息......
  • 另外,您能否提供指向您正在使用的库的 API 文档的链接?看到mi.Get 的确切声明将大有帮助。比如mi的类型是什么?
  • 是的,这是内置类型 char。据我所知,这个库没有 API 文档
  • “字符串”是什么意思? std::stringstd::basic_string&lt;char&gt; 的 typedef 名称。
  • 在大多数构建中仅供参考(如果从源代码构建,则可以调整)它被映射到 wchar_t。

标签: c++ string type-conversion mediainfo


【解决方案1】:

通读MediaInfoLib library'sC++代码,似乎有两种可能。该库定义了一个类型别名String,这就是您所看到的类型。

首先,CharString 类型中的 here is the definition

namespace MediaInfoLib {

/* ... */

//Char types
#undef  __T
#define __T(__x)     __T(__x)
#if defined(UNICODE) || defined (_UNICODE)
    typedef wchar_t Char;                    ///< Unicode/Ansi independant char
    #undef  __T
    #define __T(__x) L ## __x
#else
    typedef char Char;                       ///< Unicode/Ansi independant char
    #undef  __T
    #define __T(__x) __x
#endif

typedef std::basic_string<MediaInfoLib::Char> String;  ///< Unicode/Ansi independant string

/* ... */

}  // end namespace

如果在构建库时定义了宏UNICODE_UNICODE,则类型为std::basic_string&lt;wchar_t&gt;,即std::wstring in the standard library

要将其转换为std::string,请查看以下问题: How to convert wstring into string?

simplest answer there 使用 std::wstring_convert

如果在构建库时未定义宏UNICODE_UNICODE,则MediaInfoLib::Charchar 类型,而MediaInfoLib::String 类型是std::basic_string&lt;char&gt; 已经是std::string。也就是说,在这种情况下,返回类型是已经是 std::string

【讨论】:

    【解决方案2】:

    std::basic_string&lt;Char&gt; 转换为字符串...是的,这是内置类型 char

    如果Charchar 的别名,那么std::basic_string&lt;Char&gt; 已经是std::string。不需要转换,因为它是相同的类型。

    【讨论】:

      猜你喜欢
      • 2017-03-10
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多