英文:
Convert std::basic_string<Char> to string
问题
MediaInfoDLL返回元数据(采样率、声道、流大小、标题等)以std::basic_string<Char>格式返回,我需要将其转换为字符串,以便稍后进行处理。例如,mi.Get(Stream_Audio, 0, __T("Performer"))以std::basic_string<Char>格式返回“Artist Name”。
你能帮助我吗?
提前感谢你。
英文:
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. For example mi.Get(Stream_Audio, 0, __T("Performer")) returns "Artist Name" in std::basic_string<Char> format.
Can you help me?
Thank you in advance
答案1
得分: 2
阅读 MediaInfoLib 库的 C++ 代码,似乎有两种可能性。该库定义了类型别名 String,这就是您所看到的类型。
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; ///< 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.
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<char> is already std::string. That is, in this case, the return type is already std::string.
答案2
得分: 0
如果Char是char的别名,那么std::basic_string<Char>已经是std::string。不需要转换,因为它是相同的类型。
英文:
> Convert std::basic_string<Char> to string ... Yes, this is builtin type char
If Char is an alias of char, then std::basic_string<Char> is already std::string. No conversion is needed, since it is the same type.
答案3
得分: 0
MediaInfo 有 ZenLib 作为依赖,而 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 "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.
p.s. std::wstring_convert() was deprecated in c++17 and should probably not be used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论