public static byte[] reverseEscapeData(byte[] data) { byte[] result; try { int i = 0; Stream stream = new MemoryStream(); while (true) { if (i >= data.Length - 1) { if (data[data.Length - 2] != 125) { if (data[data.Length - 1] == 125) { throw new Exception("错误的协议无法转义!"); } stream.WriteByte(data[data.Length - 1]); } result = ToolHelper.StreamToBytes(stream); break; } if (data[i] == 125 && data[i + 1] == 2) { ++i; stream.WriteByte(126); } else if (data[i] == 125 && data[i + 1] == 1) { ++i; stream.WriteByte(125); } else { if (data[i] == 125 && data[i + 1] != 2 || data[i] == 125 && data[i + 1] != 1) { throw new Exception("错误的协议无法转义!"); } stream.WriteByte(data[i]); } ++i; } } catch { throw; } return(result); }
public static byte[] escapeData(byte[] data) { Stream stream = new MemoryStream(); byte[] result; try { int i = 0; while (true) { if (i >= data.Length) { result = ToolHelper.StreamToBytes(stream); break; } if (data[i] == 126) { stream.WriteByte(125); stream.WriteByte(2); } else if (data[i] == 125) { stream.WriteByte(125); stream.WriteByte(1); } else { stream.WriteByte(data[i]); } ++i; } } catch { throw; } return(result); }