我如何将Platform::String的内容转换为函数期望的基于char*的字符串?我假设WinRT提供了帮助函数,但我找不到它们。
谢谢!
我如何将Platform::String的内容转换为函数期望的基于char*的字符串?我假设WinRT提供了帮助函数,但我找不到它们。
谢谢!
以下是一种非常简单的方法在代码中实现此功能,无需担心缓冲区长度。 只有在确保处理的是ASCII字符时才使用此解决方案:
Platform::String^ fooRT = "aoeu";
std::wstring fooW(fooRT->Begin());
std::string fooA(fooW.begin(), fooW.end());
const char* charStr = fooA.c_str();
请记住,在此示例中,char*
存储在栈中,一旦离开作用域就会消失。
std::string
这一行会生成编译消息,有没有什么方法可以避免这种情况? “参见函数模板实例化 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string<std::_String_iterator<std::_String_val<std::_Simple_types<_Elem>>>,0>(_Iter,_Iter,const _Alloc &)'正在被编译,
其中
[
_Elem=wchar_t,
_Iter=std::_String_iterator<std::_String_val<std::_Simple_types<wchar_t>>>,
_Alloc=std::allocator<char>
]” - escape-llcPlatform::String::Data()
将返回指向字符串内容的 wchar_t const*
指针(类似于 std::wstring::c_str()
)。Platform::String
表示一个不可变字符串,因此没有访问器来获取 wchar_t*
。如果要进行更改,则需要将其内容复制到 std::wstring
中。
没有直接的方法可以获取 char*
或 char const*
,因为 Platform::String
使用宽字符(所有 Metro 样式应用程序都是 Unicode 应用程序)。可以使用 WideCharToMultiByte
进行多字节转换。
String
是不可变的,为什么 String::Begin
返回一个 char16 *
而不是 char16 const*
?使用这个指针修改单个字符是否合法? - PraetorianBegin()
和End()
都返回char16 const*
。你可以在<vccorlib.h>
中看到它们的定义。你不能修改指向的字符(字符串是引用计数的,如果有其他所有者并且你修改了字符串,那些其他所有者会非常惊讶地发现他们的字符串已经改变)。我会尽力让文档修正。感谢你的提醒。 - James McNellis不应该将宽字符转换为char,否则会破坏使用超过一个字节的语言,例如中文。以下是正确的方法。
#include <cvt/wstring>
#include <codecvt>
Platform::String^ fooRT = "foo";
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
std::string stringUtf8 = convert.to_bytes(fooRT->Data());
const char* rawCstring = stringUtf8.c_str();
char* raw = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(fooRT->Data()).c_str();
- Queststd :: string utf8 = std :: wstring_convert <std :: codecvt_utf8 <wchar_t>>().to_bytes(fooRT-> Data())
,除非您确定自己知道自己在做什么。 - Emil StyrkePlatform::String^ platform_string = p_e->Uri->AbsoluteUri;
const wchar_t* wide_chars = platform_string->Data();
char chars[512];
wcstombs(chars, wide_chars, 512);
wcstombs
generates this warning
'wcstombs': This function or variable may be unsafe. Consider using wcstombs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
- escape-llc有一个名为String::Data
的方法,返回const char16*
,这是原始的Unicode字符串。
从Unicode转换为ASCII或其他格式,即从char16*
到char*
,是另一回事。您可能不需要它,因为大多数方法现在都有它们的wchar
版本。
解决方案1:
#include <cvt/wstring>
#include <codecvt>
Platform::String^ tTextRT = "TestText";
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
std::string stringUtf8 = convert.to_bytes(tTextRT->Data());
const char* rawCstring = stringUtf8.c_str();
Platform::String^ testText = "Test Text";
std::wstring tTextW(testText->Begin());
std::string tTextA(tTextW.begin(), tTextW.end());
const char* charStr = tTextA.c_str();
#include <cvt/wstring>
#include <stringapiset.h>
Platform::String^ testText = "foo";
const wchar_t* pWStr = testText->Data();
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, NULL, 0, NULL, NULL);
char* stringUtf8 = new char[bufferSize + 1];
memset(stringUtf8, 0, bufferSize + 1);
if (0 == WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, stringUtf8, bufferSize, NULL, NULL))
{
throw std::exception("Can't convert string to Unicode");
}
const char* rawCstring = std::string(stringUtf8).c_str();
delete[] stringUtf8;