2013-02-01 19 views
3

一个在C++函数从读取资源,并返回Platform::Array<byte>^转换平台::阵列<byte>为String

库我如何转换这为Platform::Stringstd::string

BasicReaderWriter^ m_basicReaderWriter = ref new BasicReaderWriter() 
Platform::Array<byte>^ data = m_basicReaderWriter ("file.txt") 

我需要一个Platform::Stringdata

+0

对于'std :: string',至少我会想象它与迭代器对构造函数兼容。 – chris

+0

该文件的编码是什么? – svick

+0

这就是简单的ascii – ppaulojr

回答

4

如果您的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::stringPlatform::String包含的Unicode UTF-16wchar_t)字符串,所以你需要从一个转换你的包含ANSI字符串到Unicode字符串的原始字节数组。您可以使用ATL conversion helperCA2W(其中包含对Win32 API MultiByteToWideChar()的调用)进行此转换。 然后你可以使用Platform::String构造以原始UTF-16字符指针:

Platform::String^ str = ref new String(CA2W(data->begin())); 

注: 我目前还没有VS2012用,所以我还没有与C++/CX测试此代码编译器。如果你得到一些参数匹配的错误,你可能要考虑reinterpret_cast<const char*>从由data->begin()返回到char *指针byte *指针(和data->end()类似),例如转换

std::string s(reinterpret_cast<const char*>(data->begin()), data->Length); 
+0

就是这样。不能直接在WinRT中使用STL是一件复杂的事情。 – ppaulojr

相关问题