Advertisement
Ultimga

ByteMessage

Sep 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using System.Collections;
  2. using System;
  3.  
  4. public class ByteMessage
  5. {
  6. private byte[] message = new byte[0];
  7. private int readIndex = 0;
  8.  
  9. // Retrieve the message (Byte array)
  10. public byte[] GetMessage() { return message; }
  11.  
  12. // Set the message (Byte array), and reset the read index
  13. public void SetMessage(byte[] temp) { message = temp; readIndex = 0; }
  14.  
  15. // Reset the message (Byte array), and reset the read index
  16. public void ResetMessage() { message = new byte[0]; readIndex = 0; }
  17.  
  18. // Convert a variable to bytes then write them to the message
  19. public void Write(object value)
  20. {
  21. if (value is string)
  22. {
  23. string tempString = (string)value;
  24. Char[] tempChars = tempString.ToCharArray();
  25.  
  26. // First write an integer to state the length of the string
  27. byte[] bytes = BitConverter.GetBytes(tempChars.Length);
  28. BytesToMessage(bytes);
  29.  
  30. // Write each character in the string to the message
  31. for (int i = 0; i < tempChars.Length; i++)
  32. {
  33. bytes = BitConverter.GetBytes(tempChars[i]);
  34. BytesToMessage(bytes);
  35. }
  36. }
  37.  
  38. if (value is int)
  39. {
  40. byte[] bytes = BitConverter.GetBytes((int)value);
  41. BytesToMessage(bytes);
  42. }
  43.  
  44. if (value is float)
  45. {
  46. byte[] bytes = BitConverter.GetBytes((float)value);
  47. BytesToMessage(bytes);
  48. }
  49.  
  50. if (value is bool)
  51. {
  52. byte[] bytes = BitConverter.GetBytes((bool)value);
  53. BytesToMessage(bytes);
  54. }
  55. }
  56.  
  57. // Read a string from the message
  58. public string ReadString()
  59. {
  60. byte[] temp = new byte[4];
  61.  
  62. for (int i = 0; i < temp.Length; i++)
  63. {
  64. temp[i] = message[readIndex + i];
  65. }
  66.  
  67. readIndex += temp.Length;
  68.  
  69. int length = BitConverter.ToInt32(temp, 0);
  70. Char[] tempChars = new Char[length];
  71.  
  72. temp = new byte[2];
  73.  
  74. for (int i = 0; i < tempChars.Length; i++)
  75. {
  76. temp[0] = message[readIndex];
  77. temp[1] = message[readIndex + 1];
  78. readIndex += temp.Length;
  79.  
  80. tempChars[i] = BitConverter.ToChar(temp, 0);
  81. }
  82.  
  83. string finalString = new String(tempChars);
  84.  
  85. return finalString;
  86. }
  87.  
  88. // Read an int from the message
  89. public int ReadInt()
  90. {
  91. byte[] temp = new byte[4];
  92.  
  93. for (int i = 0; i < temp.Length; i++)
  94. {
  95. temp[i] = message[readIndex + i];
  96. }
  97.  
  98. readIndex += temp.Length;
  99.  
  100. return BitConverter.ToInt32(temp, 0);
  101. }
  102.  
  103. // Read a float from the message
  104. public float ReadFloat()
  105. {
  106. byte[] temp = new byte[4];
  107.  
  108. for (int i = 0; i < temp.Length; i++)
  109. {
  110. temp[i] = message[readIndex + i];
  111. }
  112.  
  113. readIndex += temp.Length;
  114.  
  115. return BitConverter.ToSingle(temp, 0);
  116. }
  117.  
  118. // Read a boolean from the message
  119. public bool ReadBool()
  120. {
  121. byte[] temp = new byte[1];
  122.  
  123. temp[0] = message[readIndex];
  124. readIndex += 1;
  125.  
  126. return BitConverter.ToBoolean(temp, 0);
  127. }
  128.  
  129. // Write bytes to the message
  130. private void BytesToMessage(byte[] bytes)
  131. {
  132. byte[] temp = new byte[message.Length + bytes.Length];
  133.  
  134. for (int i = 0; i < message.Length; i++)
  135. {
  136. temp[i] = message[i];
  137. }
  138.  
  139. for (int i = 0; i < bytes.Length; i++)
  140. {
  141. temp[message.Length + i] = bytes[i];
  142. }
  143.  
  144. message = temp;
  145. }
  146.  
  147. // Resize the array
  148. public void Resize(int _newSize)
  149. {
  150. byte[] temp = new byte[_newSize];
  151.  
  152. for (int i = 0; i < message.Length; i++)
  153. {
  154. temp[i] = message[i];
  155. }
  156.  
  157. message = temp;
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement