You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

37 lines
718 B

  1. using System;
  2. namespace MQTTnet.Serializer
  3. {
  4. public class ByteWriter
  5. {
  6. private int _index;
  7. private int _byte;
  8. public byte Value => (byte)_byte;
  9. public void Write(int @byte, int count)
  10. {
  11. for (var i = 0; i < count; i++)
  12. {
  13. var value = ((1 << i) & @byte) > 0;
  14. Write(value);
  15. }
  16. }
  17. public void Write(bool bit)
  18. {
  19. if (_index >= 8)
  20. {
  21. throw new InvalidOperationException("End of the byte reached.");
  22. }
  23. if (bit)
  24. {
  25. _byte |= 1 << _index;
  26. }
  27. _index++;
  28. }
  29. }
  30. }