将std::basic_string<Char>转换为字符串

huangapple go评论97阅读模式
英文:

Convert std::basic_string<Char> to string

问题

MediaInfoDLL返回元数据(采样率、声道、流大小、标题等)以std::basic_string&lt;Char&gt;格式返回,我需要将其转换为字符串,以便稍后进行处理。例如,mi.Get(Stream_Audio, 0, __T(&quot;Performer&quot;))std::basic_string&lt;Char&gt;格式返回“Artist Name”。

你能帮助我吗?

提前感谢你。

英文:

While MediaInfoDLL returns metadata (Sampling Rate, Channels, Stream Size, Title...) in std::basic_string&lt;Char&gt; format, I need to convert to string to be able to process it later. For example mi.Get(Stream_Audio, 0, __T(&quot;Performer&quot;)) returns "Artist Name" in std::basic_string&lt;Char&gt; format.

Can you help me?

Thank you in advance

答案1

得分: 2

阅读 MediaInfoLib 库的 C++ 代码,似乎有两种可能性。该库定义了类型别名 String,这就是您所看到的类型。

首先,这是CharString类型的定义

namespace MediaInfoLib {

/* ... */

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

typedef std::basic_string<MediaInfoLib::Char> String;  ///< Unicode/Ansi 独立字符串

/* ... */

}  // end namespace

如果在构建库时定义了宏 UNICODE_UNICODE,那么类型就是 std::basic_string<wchar_t>,这在标准库中是 std::wstring

要将其转换为 std::string,请参见这个问题:
https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string

那里的最简单的答案 使用了 std::wstring_convert

如果在构建库时未定义宏 UNICODE_UNICODE,则 MediaInfoLib::Char 类型是 char,而 MediaInfoLib::String 类型已经是 std::basic_string<char>,即 已经是 std::string

英文:

Reading through the MediaInfoLib library's C++ code, it seems there are two possibilities. The library defines a type alias String, and this is the type you're seeing.

First, here is the definition of the Char and String types:

namespace MediaInfoLib {

/* ... */

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

typedef std::basic_string&lt;MediaInfoLib::Char&gt; String;  ///&lt; Unicode/Ansi independant string

/* ... */

}  // end namespace

If the macro UNICODE or _UNICODE was defined when the library was built, then the type is std::basic_string&lt;wchar_t&gt;, which is std::wstring in the standard library.

To convert this to std::string, please see this question:
https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string

The simplest answer there uses std::wstring_convert.

If the macro UNICODE or _UNICODE was NOT defined when the library was built, then MediaInfoLib::Char is the type char, and the MediaInfoLib::String type is std::basic_string&lt;char&gt; is already std::string. That is, in this case, the return type is already std::string.

答案2

得分: 0

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

英文:

> Convert std::basic_string&lt;Char&gt; to string ... Yes, this is builtin type char

If Char is an alias of char, then std::basic_string&lt;Char&gt; is already std::string. No conversion is needed, since it is the same type.

答案3

得分: 0

MediaInfoZenLib 作为依赖,而 ZenLib 本身又有它自己的 Ztring。你可以像这样做:

#include "ZenLib/Ztring.h"
MediaInfoLib::String mediainfoString = mi.Get(Stream_Audio, 0, __T("Performer"));
ZenLib::Ztring zenlibZtring(mediainfoString);
std::string stdString = zenlibZtring.To_UTF8(); // 或者 To_Local()

你可以更加高效,直接使用 Ztring,跳过中间的 mediainfoString

附注:std::wstring_convert() 在 C++17 中已被弃用,可能 不应该 使用。

英文:

MediaInfo has ZenLib as a dependency, which in turn has its own Ztring. You can do something like this:

#include &quot;ZenLib/Ztring.h&quot;
MediaInfoLib::String mediainfoString = mi.Get(Stream_Audio, 0, __T(&quot;Performer&quot;));
ZenLib::Ztring zenlibZtring(mediainfoString);
std::string stdString = zenlibZtring.To_UTF8(); // or To_Local()

You can be slightly more efficient by directly using Ztring and skipping the intermediary mediainfoString.

p.s. std::wstring_convert() was deprecated in c++17 and should probably not be used.

huangapple
  • 本文由 发表于 2020年1月6日 02:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定