Why does BinaryWriter prepend gibberish to the start of a stream? How do you avoid it?

后端 未结 9 1731
别跟我提以往
别跟我提以往 2020-11-30 10:11

I\'m debugging some issues with writing pieces of an object to a file and I\'ve gotten down to the base case of just opening the file and writing \"TEST\" in it. I\'m doing

相关标签:
9条回答
  • 2020-11-30 10:37

    They are not byte-order marks but a length-prefix, according to MSDN:

    public virtual void Write(string value);
    

    Writes a length-prefixed string to [the] stream

    And you will need that length-prefix if you ever want to read the string back from that point. See BinaryReader.ReadString().

    Additional

    Since it seems you actually want a File-Header checker

    1. Is it a problem? You read the length-prefix back so as a type-check on the File it works OK

    2. You can convert the string to a byte[] array, probably using Encoding.ASCII. But hen you have to either use a fixed (implied) length or... prefix it yourself. After reading the byte[] you can convert it to a string again.

    3. If you had a lot of text to write you could even attach a TextWriter to the same stream. But be careful, the Writers want to close their streams. I wouldn't advice this in general, but it is good to know. Here too you will have to mark a Point where the other reader can take over (fixed header works OK).

    0 讨论(0)
  • 2020-11-30 10:39

    As Henk pointed out in this answer, this is the length of the string (as a 32-bit int).

    If you don't want this, you can either write "TEST" manually by writing the ASCII characters for each letter as bytes, or you could use:

    System.Text.Encoding.UTF8.GetBytes("TEST")
    

    And write the resulting array (which will NOT contain a length int)

    0 讨论(0)
  • 2020-11-30 10:45

    What you're seeing is actually a 7 bit encoded integer, which is a kind of integer compression.
    The BinaryWriter prepend the text with this so readers (i.e. BinaryReader) will know how long the written string is.

    • BinaryWriter.Write7BitEncodedInt
    • BinaryReader.Read7BitEncodedInt

    You can read more about the implementation details of this at http://dpatrickcaldwell.blogspot.se/2011/09/7-bit-encoding-with-binarywriter-in-net.html.

    0 讨论(0)
  • 2020-11-30 10:51

    You can save it as a UTF8 encoded byte array like this:

    ...
    
    BinaryWriter w = new BinaryWriter(fs);
    
    w.Write(UTF8Encoding.Default.GetBytes("test"));
    
    ...
    
    0 讨论(0)
  • 2020-11-30 10:53

    Sounds like byte order marks.

    http://en.wikipedia.org/wiki/Byte-order_mark

    Perhaps you want to write the string as UTF-8.

    0 讨论(0)
  • 2020-11-30 10:54

    That's a byte order mark, most likely. It's because the stream's encoding is set to Unicode.

    0 讨论(0)
提交回复
热议问题