Bit-Based BinaryWriter in C#

后端 未结 4 1916
生来不讨喜
生来不讨喜 2021-02-08 23:22

I\'m working on a bit-based B/W/Greyscale Pre-Compiled font format, and was having issues with either reading or writing the format, (I\'ve not been able to determine where the

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 00:17

    If you keep your data in a byte array (bools are of no use and take too much space, if you use them for bits, they take up a byte in memory) or in an array of a particular struct that fits your dataformat.

    Once you have an internal memory representation, you don't need a bit-based binary writer anymore. You can simply write the data to a BinaryWriter and you're done with it.

    ...but the default .Net 4.0 BinaryWriter writes a Boolean value as a full byte, and as you can imagine, that negates the use of a bit-based format....

    The reason for this is: the bool is, by definition, of 1 byte size in C#. The BinaryWriter simply writes what you give it.

提交回复
热议问题