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

后端 未结 9 1751
别跟我提以往
别跟我提以往 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: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.

提交回复
热议问题