2013-04-23 113 views
1

我是新来的c + +和UnicodeString转换为字符串有问题,所以现在搜索最简单的方法从一种类型转换为其他。在C++中将UnicodeString转换为const char *的最简单方法是什么?

我想使用基本的窗口函数,它需要字符串与UnicodeString,如何使代码工作?使用

UnicodeString Exec = "notepad"; 
WinExec(Exec.c_str(), 0); 

环境是C++ Builder的XE2

+1

['WideCharToMultiByte()'](http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130(v = vs.85).aspx)可能是你在找什么对于。仔细读。这是相当直接的。 – WhozCraig 2013-04-23 06:33:26

+0

'wstring_convert'不坏(http://en.cppreference.com/w/cpp/locale/wstring_convert)。 – 2014-06-11 21:15:51

回答

3

std::string不能存储Unicode数据。你将需要一个std :: wstring。

我从来没有听说过的UnicodeString之前,但看着这里的API:

http://docwiki.embarcadero.com/Libraries/XE2/en/System.UnicodeString_Methods

它有一个叫做.c_str()函数返回一个wchar_t的*您可以再使用来构建a std::wstring

如果你确实需要一个std :: string,那么看看这个答案。

How to convert wstring into string?

+1

[wchar_t不是Unicode](http://en.wikipedia.org/wiki/Wide_character#C.2FC.2B.2B),你不能依赖'std :: wstring'的大小来存储Unicode字符。如果可能的话,适当地使用'std :: u16string'或'std :: u32string'。 – 2013-04-23 06:39:21

+0

是的,道歉,wchar_t'may'可以存储unicode字符,但它的大小是依赖于编译器的。谢谢@Captain Obvlious – Salgar 2013-04-23 06:46:25

相关问题