Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

like image 902
user3204810 Avatar asked Oct 17 '25 14:10

user3204810


1 Answers

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: 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.

like image 95
NicholasM Avatar answered Oct 19 '25 05:10

NicholasM