Convert Platform::Array<byte> to String
c++, c++-cx, string, windows-runtime
Solution
If your `Platform::Array<byte>^ data` contains an ASCII string (as you clarified in a comment to your question), you can convert it to `std::string` using proper `std::string` constructor overloads (note that `Platform::Array` offers STL-like `begin()` and `end()` methods):
// 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 );
Unlike `std::string`, `Platform::String` contains Unicode UTF-16 (`wchar_t`) strings, so you need a conversion from your original byte array containing the ANSI string to Unicode string. You can perform this conversion using ATL conversion helper class `CA2W` (which wraps calls to Win32 API `MultiByteToWideChar()`). Then you can use `Platform::String` constructor taking a raw UTF-16 character pointer:
Platform::String^ str = ref new String( CA2W( data->begin() ) );
Note: I currently don't have VS2012 available, so I haven't tested this code with the C++/CX compiler. If you get some argument matching errors, you may want to consider `reinterpret_cast<const char*>` to convert from the `byte *` pointer returned by `data->begin()` to a `char *` pointer (and similar for `data->end()`), e.g.
std::string s( reinterpret_cast<const char*>(data->begin()), data->Length );
Problem
A have a function in C++ from a library that reads a resource and returns `Platform::Array<byte>^` How can I convert this into a `Platform::String` or an `std::string` ``` BasicReaderWriter^ m_basicReaderWriter = ref new BasicReaderWriter() Platform::Array<byte>^ data = m_basicReaderWriter ("file.txt") ``` I need a `Platform::String` from `data`