locked
DataWriter->WriteString emitting terminating \0

    Question

  • I working on sending data via UDP and came across an issue where my data was coming out misaligned. Digging into it I found that DataWriter.WriteString was writing out a terminating 0x00 byte. Is this expected and is there a way from preventing this from occurring? In my code I'm sending a String^ variable that was converted from a std::wstring, if that make any difference. I'm porting over some WinRT C# code which does not have this problem.

    Thursday, September 12, 2013 1:46 AM

Answers

  • all you really need is 

    std::fstream outfile("output.bin", std::ios::binary);
    

    now you can use a class object of any kind that has overloaded the << and >> operators


    Corsair Carbide 300R with window & Corsair TX850V2 70A@12V

    Asus M5A99FX PRO R2.0 CFX/SLI & AMD Phenom II 965 C3 Black Edition @ 4.0 GHz & G.SKILL RipjawsX DDR3-2133 8 GB 

    GTX 260 SLI 216 core (GT200 Tesla) & Asus PA238QR IPS LED HDMI DP 1080p

    ST2000DM001 & Windows 8.1 Enterprise x64

    Microsoft Wireless Desktop 2000 & Wacom Bamboo CHT470M

    Place your rig specifics into your signature like I have, makes it 100x easier to understand!


    Hardcore Games Legendary is the Only Way to Play

    Thursday, September 19, 2013 3:16 PM

All replies

  • This is expected.  Strings are null terminated.  You could send a Byte stream instead if you don't want the null.


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, September 12, 2013 7:05 PM
    Moderator
  • Why does the same function with C# using the string class not emit a null terminator?
    Thursday, September 12, 2013 7:44 PM
  • Please share a repro of both situations

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, September 12, 2013 8:21 PM
    Moderator
  • Strings are null terminated so the programming language knows how long the string is.

    Its also useful if sending numerous strings to a file. It makes reading them back in possible as you know where each string ends.


    n.Wright

    Thursday, September 12, 2013 8:24 PM
  • Strings are null terminated so the programming language knows how long the string is.

    Its also useful if sending numerous strings to a file. It makes reading them back in possible as you know where each string ends.


    n.Wright


    Thanks for the reply. I'm well versed in the structure of character strings and their null termination. The problem I'm battling here is a difference between behavior of DataWriter::WriteString between C++/CX and C#. C++ is emitting a null character and the C# call is not. Apparently C# strings do not have a null terminator (which make sense since they are a true object with a Length property) and this may explain the null character not being emitted. I'm writing this data to the network and I do not want the null terminator being sent (the length is being sent ahead of the data). As first suggested I could just write out a byte stream, but that would require another conversion from Unicode characters to bytes.
    Thursday, September 12, 2013 9:18 PM
  • There must be something sent out with a string to say how long it is.

    Either a null termination or a length specifier.

    Otherwise the string cant be read back from a file.

    I know wpf C# sends out the length as  I can read in strings separately.


    n.Wright

    Thursday, September 12, 2013 9:23 PM
  • There must be something sent out with a string to say how long it is.

    Either a null termination or a length specifier.

    Otherwise the string cant be read back from a file.

    I know wpf C# sends out the length as  I can read in strings separately.


    n.Wright

    I'm porting over some code from C# to C++ and the part I'm having issues with is basically a direct copy of the code, barring language differences. I'll look closer at it his evening and repo both cases, but I'll probably just punt on this issue and use WriteBytes.

    Thursday, September 12, 2013 9:45 PM
  • If anyone is interested I solve the problem using the following:

    Copy character data from std::string to std::vector<char>. This was necessary because std::string only supplies a const char*.

    This gave me access to a char* that I then could use to populate Platform::Array<UCHAR>. Because the null terminator was copied to the std::vector object, I had to copy std::vector::size() -1 characters into the WinRT array.

    Called DataWriter::WriteBytes with my Platform::Array<UCHAR> object.

    Friday, September 13, 2013 2:47 PM
  • all you really need is 

    std::fstream outfile("output.bin", std::ios::binary);

    now you can use a class object of any kind that has overloaded the << and >> operators



    I'm pretty sure that STL I/O is not allowed in Windows Store Applications.
    Thursday, September 19, 2013 3:55 PM