How to do data conversion in Windows Store app

Q: How to convert streams in .NET?

A: Here is the list to show the different stream types conversion.

Conversion between Stream and IRandomAccessStream

// stream to IRandomAccessStream

var randomAccessStream = new InMemoryRandomAccessStream();

var outputStream = randomAccessStream.GetOutputStreamAt(0);

await RandomAccessStream.CopyAsync(stream.AsInputStream(), outputStream);


// IRandomAccessStream to stream

Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));


Conversion between IBuffer and Stream

// buffer to stream

Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);


// stream to buffer

MemoryStream memoryStream = new MemoryStream();           

 if (stream != null)


      byte[] bytes = ReadFully(stream);

      if (bytes != null)


           var binaryWriter = new BinaryWriter(memoryStream);




 IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length);


Conversion between IRandomAccessStream and FileInputStream/FileOutputStream

FileInputStream inputStream = randomStream.GetInputStreamAt(0) as FileInputStream;


FileOutputStream outStream = randomStream.GetOutputStreamAt(0) as FileOutputStream;


Related MSDN Forum Threads


Q: How to convert IBuffer and COM IStream in C++/CX?

A: Conversion between IBuffer and COM IStream

// IBuffer to IStream

IStream* createIStreamFromIBuffer(Streams::IBuffer ^buffer)  {

    // convert the IBuffer into an IStream to be used with WIC

    IStream *fileContentsStream;

    HRESULT res = CreateStreamOnHGlobal(NULL, TRUE, &fileContentsStream);

    if (FAILED(res) || !fileContentsStream) {

        throw ref new FailureException();


    Streams::DataReader^ dataReader = Streams::DataReader::FromBuffer(buffer);

    // read the data into the stream in chunks of 1MB to preserve memory

    while (dataReader->UnconsumedBufferLength > 0) {

        UINT chunkSize = min(1024*1024, dataReader->UnconsumedBufferLength);

        auto data = ref new Platform::Array<uint8>(chunkSize);


        ULONG written;

        res = fileContentsStream->Write(data->Data, chunkSize, &written);

        if (FAILED(res) || written != chunkSize) {


            throw ref new FailureException();



    return fileContentsStream;



//Com buffer to IBufferByteAccess

IUnknown* pUnk = reinterpret_cast<IUnknown*>(buffer);


IBufferByteAccess* pBufferByteAccess = nullptr;

HRESULT hr = pUnk->QueryInterface(IID_PPV_ARGS(&pBufferByteAccess);

Related MSDN Forum Threads


Q: How to convert native char* and String^ in C++/CX?

A: Conversion between String^ and Char*

// String^ to char*

String^ str1 = "AAAAAAAA";

wstring wstr( str1->Data() );

int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);

char* buffer = new char[len + 1]; 

WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);

buffer[len] = '\0';


//Char* to String^

std::wstring s2ws(const std::string& s)


    int len;

    int slength = (int)s.length() + 1;

    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);

    wchar_t* buf = new wchar_t[len];

    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);

    std::wstring r(buf);

    delete[] buf;

    return r;



String ^statusString = ref new String( s2ws(statusHelper).c_str());

Related MSDN Forum Threads 

Skip to main content