/// <summary> /// Returns the selected bytes from the COMPLETE Packet, starting at Offset, that is Count bytes long. /// Similar to String.Substring() method. /// /// if Offset is LONGER than the packet size, an array of nulls (/0) of length Count will be returned. /// /// If the count is LONGER than the packet size, but the offset is IN the packet size, the data will be returned where is available, and then proceeded by trailing Nulls (/0). /// </summary> /// <param name="Offset"></param> /// <param name="Count"></param> /// <returns></returns> public byte[] _GetRawBytes(int Offset, int Count) { if (Count < 0) { //give back nothing. return(new byte[0]); } if (Offset < 0) { //wrap the offset to account for negative values... while (Offset < 0) { Offset += _RawPacketBytes.Length; } } if (Offset > _RawPacketBytes.Length) { //trying to get data past the end of the string, return "nulls". return(Bits.EmptyBits(Count)); } if (_RawPacketBytes.Length >= Offset + Count) { //Enough data exists at this point to return the data in full. return(_RawPacketBytes.Skip(Offset).Take(Count).ToArray()); } else { //not quite enough data exists past this point to return data, get what you can, and then add nulls. return(Bits.ArrayCombine(_RawPacketBytes.Skip(Offset).ToArray(), new byte[Count - (_RawPacketBytes.Length - Offset)])); } }
/// <summary> /// Sets the selected bytes of the COMPLETE Packet, starting at Offset, to the Arrays contents. /// /// If the result is LONGER than the original packet, the packet is resized. /// If the offset is LONGER than the packet, the packet is resized to fit. /// </summary> /// <param name="Offset"></param> /// <param name="Array"></param> /// <returns></returns> public bool _SetRawBytes(int Offset, byte[] Array) { if (Offset < 0) { //wrap the offset to account for negative values... while (Offset < 0) { Offset += _RawPacketBytes.Length; } } if (Offset > _RawPacketBytes.Length) { //trying to append AFTER the length of the data! Add extra! _RawPacketBytes = Bits.ArrayCombine(_RawPacketBytes.Take(Offset).ToArray(), Bits.EmptyBits(Offset - _RawPacketBytes.Length), Array); return(true); } if (_RawPacketBytes.Length >= Offset + Array.Length) { //long enough to overwrite... //return _RawDataBytes.Skip(Start).Take(Count).ToArray(); _RawPacketBytes = Bits.ArrayCombine(_RawPacketBytes.Take(Offset).ToArray(), Array, _RawPacketBytes.Skip(Offset).Skip(Array.Length).ToArray()); return(true); } else { //not long enough to overwrite, but long enough to start adding part way through... _RawPacketBytes = Bits.ArrayCombine(_RawPacketBytes.Take(Offset).ToArray(), Array); return(true); } }
public bool Initialise() { Data = Bits.Repeat("\0", 108); Data = Bits.ArrayCombine(Data, new byte[16] { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }); Data = Bits.ArrayCombine(Data, Bits.Repeat("\0", 48)); return(true); }