如果您Platform::Array<byte>^ data包含一个 ASCII 字符串(正如您在对问题的评论中阐明的那样),您可以将其转换为std::string使用适当的std::string构造函数重载(请注意,它Platform::Array提供了类似 STL 的begin()方法end()):
// Using std::string's range constructor
std::string s( data->begin(), data->end() );
// Using std::string's buffer pointer + length constructor
std::string s( data->begin(), data->Length );
与std::string,Platform::String包含Unicode UTF-16 ( wchar_t) 字符串不同,因此您需要将包含 ANSI 字符串的原始字节数组转换为 Unicode 字符串。您可以使用ATL 转换帮助程序类CA2W(包装对 Win32 API 的调用MultiByteToWideChar())执行此转换。然后您可以使用Platform::String构造函数获取原始 UTF-16 字符指针:
Platform::String^ str = ref new String( CA2W( data->begin() ) );
注意:
我目前没有可用的 VS2012,所以我没有用 C++/CX 编译器测试过这段代码。如果您遇到一些参数匹配错误,您可能需要考虑从返回reinterpret_cast<const char*>的指针转换为指针(类似 for ),例如byte *data->begin()char *data->end()
std::string s( reinterpret_cast<const char*>(data->begin()), data->Length );