BigEndianWriter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.IO;
  3. namespace Utilities
  4. {
  5. public class BigEndianWriter
  6. {
  7. public static void WriteInt16(byte[] buffer, int offset, short value)
  8. {
  9. byte[] bytes = BigEndianConverter.GetBytes(value);
  10. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  11. }
  12. public static void WriteInt16(byte[] buffer, ref int offset, short value)
  13. {
  14. WriteInt16(buffer, offset, value);
  15. offset += 2;
  16. }
  17. public static void WriteUInt16(byte[] buffer, int offset, ushort value)
  18. {
  19. byte[] bytes = BigEndianConverter.GetBytes(value);
  20. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  21. }
  22. public static void WriteUInt16(byte[] buffer, ref int offset, ushort value)
  23. {
  24. WriteUInt16(buffer, offset, value);
  25. offset += 2;
  26. }
  27. public static void WriteUInt24(byte[] buffer, int offset, uint value)
  28. {
  29. byte[] bytes = BigEndianConverter.GetBytes(value);
  30. Array.Copy(bytes, 1, buffer, offset, 3);
  31. }
  32. public static void WriteUInt24(byte[] buffer, ref int offset, uint value)
  33. {
  34. WriteUInt24(buffer, offset, value);
  35. offset += 3;
  36. }
  37. public static void WriteInt32(byte[] buffer, int offset, int value)
  38. {
  39. byte[] bytes = BigEndianConverter.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 WriteUInt32(byte[] buffer, int offset, uint value)
  48. {
  49. byte[] bytes = BigEndianConverter.GetBytes(value);
  50. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  51. }
  52. public static void WriteUInt32(byte[] buffer, ref int offset, uint value)
  53. {
  54. WriteUInt32(buffer, offset, value);
  55. offset += 4;
  56. }
  57. public static void WriteInt64(byte[] buffer, int offset, long value)
  58. {
  59. byte[] bytes = BigEndianConverter.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 WriteUInt64(byte[] buffer, int offset, ulong value)
  68. {
  69. byte[] bytes = BigEndianConverter.GetBytes(value);
  70. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  71. }
  72. public static void WriteUInt64(byte[] buffer, ref int offset, ulong value)
  73. {
  74. WriteUInt64(buffer, offset, value);
  75. offset += 8;
  76. }
  77. public static void WriteGuidBytes(byte[] buffer, int offset, Guid value)
  78. {
  79. byte[] bytes = BigEndianConverter.GetBytes(value);
  80. Array.Copy(bytes, 0, buffer, offset, bytes.Length);
  81. }
  82. public static void WriteGuidBytes(byte[] buffer, ref int offset, Guid value)
  83. {
  84. WriteGuidBytes(buffer, offset, value);
  85. offset += 16;
  86. }
  87. public static void WriteInt16(Stream stream, short value)
  88. {
  89. byte[] bytes = BigEndianConverter.GetBytes(value);
  90. stream.Write(bytes, 0, bytes.Length);
  91. }
  92. public static void WriteUInt16(Stream stream, ushort value)
  93. {
  94. byte[] bytes = BigEndianConverter.GetBytes(value);
  95. stream.Write(bytes, 0, bytes.Length);
  96. }
  97. public static void WriteUInt24(Stream stream, uint value)
  98. {
  99. byte[] bytes = BigEndianConverter.GetBytes(value);
  100. stream.Write(bytes, 1, 3);
  101. }
  102. public static void WriteInt32(Stream stream, int value)
  103. {
  104. byte[] bytes = BigEndianConverter.GetBytes(value);
  105. stream.Write(bytes, 0, bytes.Length);
  106. }
  107. public static void WriteUInt32(Stream stream, uint value)
  108. {
  109. byte[] bytes = BigEndianConverter.GetBytes(value);
  110. stream.Write(bytes, 0, bytes.Length);
  111. }
  112. public static void WriteInt64(Stream stream, long value)
  113. {
  114. byte[] bytes = BigEndianConverter.GetBytes(value);
  115. stream.Write(bytes, 0, bytes.Length);
  116. }
  117. public static void WriteUInt64(Stream stream, ulong value)
  118. {
  119. byte[] bytes = BigEndianConverter.GetBytes(value);
  120. stream.Write(bytes, 0, bytes.Length);
  121. }
  122. public static void WriteGuidBytes(Stream stream, Guid value)
  123. {
  124. byte[] bytes = BigEndianConverter.GetBytes(value);
  125. stream.Write(bytes, 0, bytes.Length);
  126. }
  127. }
  128. }