[英]Convert std::basic_string<Char> to string
While MediaInfoDLL returns metadata (Sampling Rate, Channels, Stream Size, Title...) in std::basic_string<Char>
format, I need to convert to string to be able to process it later.虽然 MediaInfoDLL 以std::basic_string<Char>
格式返回元数据(采样率、通道、流大小、标题...),但我需要转换为字符串以便以后处理它。 For example mi.Get(Stream_Audio, 0, __T("Performer"))
returns "Artist Name" in std::basic_string<Char>
format.例如mi.Get(Stream_Audio, 0, __T("Performer"))
以std::basic_string<Char>
格式返回“艺术家姓名”。
Can you help me?你能帮助我吗?
Thank you in advance先感谢您
Reading through the MediaInfoLib library's C++ code, it seems there are two possibilities.通读 MediaInfoLib 库的C++ 代码,似乎有两种可能性。 The library defines a type alias String
, and this is the type you're seeing.该库定义了一个类型别名String
,这就是您看到的类型。
First, here is the definition of the Char
and String
types:首先, 这里是Char
和String
类型的定义:
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
If the macro UNICODE
or _UNICODE
was defined when the library was built, then the type is std::basic_string<wchar_t>
, which is std::wstring
in the standard library .如果在构建库时定义了宏UNICODE
或_UNICODE
,则类型为std::basic_string<wchar_t>
,即标准库中的std::wstring
。
To convert this to std::string
, please see this question: How to convert wstring into string?要将其转换为std::string
,请参阅此问题: 如何将 wstring 转换为字符串?
The simplest answer there uses std::wstring_convert
.最简单的答案是使用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<char>
is already std::string
.如果在构建库时未定义宏UNICODE
或_UNICODE
,则MediaInfoLib::Char
是char
类型,而MediaInfoLib::String
类型是std::basic_string<char>
已经是std::string
。 That is, in this case, the return type is already std::string .也就是说,在这种情况下,返回类型已经是std::string 。
Convert
std::basic_string<Char>
to string ... Yes, this is builtin type char将std::basic_string<Char>
转换为 string ... 是的,这是内置类型 char
If Char
is an alias of char
, then std::basic_string<Char>
is already std::string
.如果Char
是char
的别名,则std::basic_string<Char>
已经是std::string
。 No conversion is needed, since it is the same type.不需要转换,因为它是相同的类型。
MediaInfo
has ZenLib
as a dependency, which in turn has its own Ztring
. MediaInfo
将ZenLib
作为依赖项,而Ztring
又拥有自己的Ztring
。 You can do something like this:你可以这样做:
#include "ZenLib/Ztring.h"
MediaInfoLib::String mediainfoString = mi.Get(Stream_Audio, 0, __T("Performer"));
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
.通过直接使用Ztring
并跳过中间mediainfoString
可以稍微提高效率。
ps std::wstring_convert()
was deprecated in c++17 and should probably not be used. ps std::wstring_convert()
在 c++17 中已弃用,可能不应使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.