.NET Standard 2.0 Library for manipulating binary data
The library supports reading and writing
- Induvidual bits
- Integers of variable bit length
- Signed Byte
- Signed Short
- Signed Int
- Signed Long
- Unsigned Byte
- Unsigned Short
- Unsigned Int
- Unsigned Long
- Half-precision floating-point numbers
- Float
- Double
- Char
- String
- Arrays and Lists of everything above.
You can read and write to the same stream, making it easy to descramble data without the need of multiple streams.
BinaryStreamstream=newBinaryStream();// if you dont provide it with a stream it'll use a MemoryStream internally.stream.BitOrder=Endianness.LittleEndian;// you can set both bit and byte order like thisstream.ByteOrder=Endianness.LittleEndian;// or through the constructorstream.DefaultTextEncoding=TextEncoding.UTF8;// what text encoding should we use as default, // this can be set through the constructor.BinaryReaderread=stream.Read;// you can still use stream.Read.TypeBinaryWriterwrite=stream.Write;// i just think this makes my code easier to readbyte[]bytes=newbyte[]{1,2,4,7};int[]ints=newint[]{-1,200,7600,984,-999};List<float>floats=newList<float>(newfloat[]{1f,-2f,0.55f});// writewrite.ULong(123456);write.ByteArray(bytes);write.IntArrayAsBits(ints,14);write.HalfList(floats);write.Bit(1);// a bit is represented by a bytewrite.Bit(0);write.Bit(1);write.Bit(0);write.String("test");// reset posstream.Flush();// it's important to flush if you write bitsstream.ByteOffset=0;// changing offset will also flush dirty bitsstream.BitOffset=0;// writing data that is not aligned to a byte is a lot slower// readread.ULong();read.ByteArray(4);read.ShortListFromBits(ints.Length,14);// we wrote ints, but can read shortsread.HalfArray(floats.Count);// halfs are converted and returned as floatsread.BitArray(4);read.String(4);// seekstream.BitOffset+=200;stream.BitOffset--;stream.ByteOffset=55;stream.ByteOffset++;boolend=stream.EndOfStream;longlen=stream.Length;