BigEndianWriter.cs 5.0 KB

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