Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System;
- public class ByteMessage
- {
- private byte[] message = new byte[0];
- private int readIndex = 0;
- // Retrieve the message (Byte array)
- public byte[] GetMessage() { return message; }
- // Set the message (Byte array), and reset the read index
- public void SetMessage(byte[] temp) { message = temp; readIndex = 0; }
- // Reset the message (Byte array), and reset the read index
- public void ResetMessage() { message = new byte[0]; readIndex = 0; }
- // Convert a variable to bytes then write them to the message
- public void Write(object value)
- {
- if (value is string)
- {
- string tempString = (string)value;
- Char[] tempChars = tempString.ToCharArray();
- // First write an integer to state the length of the string
- byte[] bytes = BitConverter.GetBytes(tempChars.Length);
- BytesToMessage(bytes);
- // Write each character in the string to the message
- for (int i = 0; i < tempChars.Length; i++)
- {
- bytes = BitConverter.GetBytes(tempChars[i]);
- BytesToMessage(bytes);
- }
- }
- if (value is int)
- {
- byte[] bytes = BitConverter.GetBytes((int)value);
- BytesToMessage(bytes);
- }
- if (value is float)
- {
- byte[] bytes = BitConverter.GetBytes((float)value);
- BytesToMessage(bytes);
- }
- if (value is bool)
- {
- byte[] bytes = BitConverter.GetBytes((bool)value);
- BytesToMessage(bytes);
- }
- }
- // Read a string from the message
- public string ReadString()
- {
- byte[] temp = new byte[4];
- for (int i = 0; i < temp.Length; i++)
- {
- temp[i] = message[readIndex + i];
- }
- readIndex += temp.Length;
- int length = BitConverter.ToInt32(temp, 0);
- Char[] tempChars = new Char[length];
- temp = new byte[2];
- for (int i = 0; i < tempChars.Length; i++)
- {
- temp[0] = message[readIndex];
- temp[1] = message[readIndex + 1];
- readIndex += temp.Length;
- tempChars[i] = BitConverter.ToChar(temp, 0);
- }
- string finalString = new String(tempChars);
- return finalString;
- }
- // Read an int from the message
- public int ReadInt()
- {
- byte[] temp = new byte[4];
- for (int i = 0; i < temp.Length; i++)
- {
- temp[i] = message[readIndex + i];
- }
- readIndex += temp.Length;
- return BitConverter.ToInt32(temp, 0);
- }
- // Read a float from the message
- public float ReadFloat()
- {
- byte[] temp = new byte[4];
- for (int i = 0; i < temp.Length; i++)
- {
- temp[i] = message[readIndex + i];
- }
- readIndex += temp.Length;
- return BitConverter.ToSingle(temp, 0);
- }
- // Read a boolean from the message
- public bool ReadBool()
- {
- byte[] temp = new byte[1];
- temp[0] = message[readIndex];
- readIndex += 1;
- return BitConverter.ToBoolean(temp, 0);
- }
- // Write bytes to the message
- private void BytesToMessage(byte[] bytes)
- {
- byte[] temp = new byte[message.Length + bytes.Length];
- for (int i = 0; i < message.Length; i++)
- {
- temp[i] = message[i];
- }
- for (int i = 0; i < bytes.Length; i++)
- {
- temp[message.Length + i] = bytes[i];
- }
- message = temp;
- }
- // Resize the array
- public void Resize(int _newSize)
- {
- byte[] temp = new byte[_newSize];
- for (int i = 0; i < message.Length; i++)
- {
- temp[i] = message[i];
- }
- message = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement