简体   繁体   English

转换 std::basic_string<Char> 串起来

[英]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:首先, 这里是CharString类型的定义

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::Charchar类型,而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 charstd::basic_string<Char>转换为 string ... 是的,这是内置类型 char

If Char is an alias of char , then std::basic_string<Char> is already std::string .如果Charchar的别名,则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 . MediaInfoZenLib作为依赖项,而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.

相关问题 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment 无法转换&#39;std :: string {aka std :: basic_string <char> }&#39;为&#39;char&#39;作为回报 - Cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:无法转换std :: vector <std::basic_string<char> &gt;到std :: string * - error: cannot convert std::vector<std::basic_string<char> > to std::string* 无法从&#39;const std :: __ 1 :: basic_string转换 <char> 到&#39;std :: __ 1 :: basic_string <char> *” - No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *' 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM