LittleEndianWriter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. namespace Utilities
  4. {
  5. public class LittleEndianWriter
  6. {
  7. public static void WriteUInt16(byte[] buffer, int offset, ushort value)
  8. {
  9. byte[] bytes = LittleEndianConverter.GetBytes(value);
  10. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  11. }
  12. public static void WriteUInt16(byte[] buffer, ref int offset, ushort value)
  13. {
  14. WriteUInt16(buffer, offset, value);
  15. offset += 2;
  16. }
  17. public static void WriteInt16(byte[] buffer, int offset, short value)
  18. {
  19. byte[] bytes = LittleEndianConverter.GetBytes(value);
  20. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  21. }
  22. public static void WriteInt16(byte[] buffer, ref int offset, short value)
  23. {
  24. WriteInt16(buffer, offset, value);
  25. offset += 2;
  26. }
  27. public static void WriteUInt32(byte[] buffer, int offset, uint value)
  28. {
  29. byte[] bytes = LittleEndianConverter.GetBytes(value);
  30. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  31. }
  32. public static void WriteUInt32(byte[] buffer, ref int offset, uint value)
  33. {
  34. WriteUInt32(buffer, offset, value);
  35. offset += 4;
  36. }
  37. public static void WriteInt32(byte[] buffer, int offset, int value)
  38. {
  39. byte[] bytes = LittleEndianConverter.GetBytes(value);
  40. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  41. }
  42. public static void WriteInt32(byte[] buffer, ref int offset, int value)
  43. {
  44. WriteInt32(buffer, offset, value);
  45. offset += 4;
  46. }
  47. public static void WriteUInt64(byte[] buffer, int offset, ulong value)
  48. {
  49. byte[] bytes = LittleEndianConverter.GetBytes(value);
  50. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  51. }
  52. public static void WriteUInt64(byte[] buffer, ref int offset, ulong value)
  53. {
  54. WriteUInt64(buffer, offset, value);
  55. offset += 8;
  56. }
  57. public static void WriteInt64(byte[] buffer, int offset, long value)
  58. {
  59. byte[] bytes = LittleEndianConverter.GetBytes(value);
  60. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  61. }
  62. public static void WriteInt64(byte[] buffer, ref int offset, long value)
  63. {
  64. WriteInt64(buffer, offset, value);
  65. offset += 8;
  66. }
  67. public static void WriteGuidBytes(byte[] buffer, int offset, Guid value)
  68. {
  69. byte[] bytes = LittleEndianConverter.GetBytes(value);
  70. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  71. }
  72. public static void WriteGuidBytes(byte[] buffer, ref int offset, Guid value)
  73. {
  74. WriteGuidBytes(buffer, offset, value);
  75. offset += 16;
  76. }
  77. public static void WriteUInt16(Stream stream, ushort value)
  78. {
  79. byte[] bytes = LittleEndianConverter.GetBytes(value);
  80. stream.Write(bytes, 0, bytes.Length);
  81. }
  82. public static void WriteInt32(Stream stream, int value)
  83. {
  84. byte[] bytes = LittleEndianConverter.GetBytes(value);
  85. stream.Write(bytes, 0, bytes.Length);
  86. }
  87. public static void WriteUInt32(Stream stream, uint value)
  88. {
  89. byte[] bytes = LittleEndianConverter.GetBytes(value);
  90. stream.Write(bytes, 0, bytes.Length);
  91. }
  92. }
  93. }