public static byte[] Construct(ushort header, params object[] values) { byte[] body = GetBytes(values); var buffer = new byte[6 + body.Length]; byte[] headerData = BigEndian.GetBytes(header); byte[] lengthData = BigEndian.GetBytes(2 + body.Length); Buffer.BlockCopy(lengthData, 0, buffer, 0, 4); Buffer.BlockCopy(headerData, 0, buffer, 4, 2); Buffer.BlockCopy(body, 0, buffer, 6, body.Length); return(buffer); }
public static byte[] GetBytes(params object[] values) { var buffer = new List <byte>(); foreach (object value in values) { switch (Type.GetTypeCode(value.GetType())) { case TypeCode.Byte: buffer.Add((byte)value); break; case TypeCode.Boolean: buffer.Add(Convert.ToByte((bool)value)); break; case TypeCode.Int32: buffer.AddRange(BigEndian.GetBytes((int)value)); break; case TypeCode.UInt16: buffer.AddRange(BigEndian.GetBytes((ushort)value)); break; default: case TypeCode.String: { byte[] data = value as byte[]; if (data == null) { string stringValue = value.ToString() .Replace("\\a", "\a").Replace("\\b", "\b") .Replace("\\f", "\f").Replace("\\n", "\n") .Replace("\\r", "\r").Replace("\\t", "\t") .Replace("\\v", "\v").Replace("\\0", "\0"); data = BigEndian.GetBytes(stringValue); } buffer.AddRange(data); break; } } } return(buffer.ToArray()); }
public static byte[] ToBytes(string value) { value = value.Replace("{b:}", "[0]") .Replace("{u:}", "[0][0]") .Replace("{s:}", "[0][0]") .Replace("{i:}", "[0][0][0][0]"); for (int i = 0; i <= 13; i++) { value = value.Replace( "[" + i + "]", ((char)i).ToString()); } string objectValue = string.Empty; while (!string.IsNullOrWhiteSpace( objectValue = value.GetChild("{b:").GetParent("}"))) { char byteChar = objectValue.ToLower()[0]; byte byteValue = (byte)(byteChar == 't' || (byteChar == '1' && objectValue.Length == 1) ? 1 : 0); if (byteChar != 'f' && byteValue != 1 && !byte.TryParse(objectValue.ToLower(), out byteValue)) { break; } string byteParam = $"{{b:{objectValue}}}"; value = value.Replace(byteParam, ((char)byteValue).ToString()); } while (!string.IsNullOrWhiteSpace( objectValue = value.GetChild("{u:").GetParent("}"))) { ushort shortValue = 0; if (!ushort.TryParse(objectValue, out shortValue)) { break; } byte[] ushortData = BigEndian.GetBytes(shortValue); string ushortParam = $"{{u:{objectValue}}}"; value = value.Replace(ushortParam, Encoding.Default.GetString(ushortData)); } while (!string.IsNullOrWhiteSpace( objectValue = value.GetChild("{i:").GetParent("}"))) { int intValue = 0; if (!int.TryParse(objectValue, out intValue)) { break; } byte[] intData = BigEndian.GetBytes(intValue); string intParam = $"{{i:{objectValue}}}"; value = value.Replace(intParam, Encoding.Default.GetString(intData)); } while (!string.IsNullOrWhiteSpace( objectValue = value.GetChild("{s:").GetParent("}"))) { byte[] stringData = BigEndian.GetBytes(objectValue); string stringParam = $"{{s:{objectValue}}}"; value = value.Replace(stringParam, Encoding.Default.GetString(stringData)); } if (value.StartsWith("{l}") && value.Length >= 5) { byte[] lengthData = BigEndian.GetBytes(value.Length - 3); value = Encoding.Default.GetString(lengthData) + value.Substring(3); } return(Encoding.Default.GetBytes(value)); }
public void WriteString(string value, int position) { byte[] encoded = BigEndian.GetBytes(value); WriteObject(encoded, value, position); }
public void WriteBoolean(bool value, int position) { byte[] encoded = BigEndian.GetBytes(value); WriteObject(encoded, value, position); }
public void WriteShort(ushort value, int position) { byte[] encoded = BigEndian.GetBytes(value); WriteObject(encoded, value, position); }
public void WriteInteger(int value, int position) { byte[] encoded = BigEndian.GetBytes(value); WriteObject(encoded, value, position); }
public static byte[] ToBytes(string packet) { for (int i = 0; i <= 13; i++) { packet = packet.Replace( "[" + i + "]", ((char)i).ToString()); } MatchCollection matches = _valueGrabber.Matches(packet); foreach (Match match in matches) { string type = match.Groups["type"].Value; string value = match.Groups["value"].Value; byte[] data = null; #region Switch: type switch (type) { case "s": { data = BigEndian.GetBytes(value); break; } case "u": { ushort uValue = 0; ushort.TryParse(value, out uValue); data = BigEndian.GetBytes(uValue); break; } case "i": { int iValue = 0; int.TryParse(value, out iValue); data = BigEndian.GetBytes(iValue); break; } case "b": { byte bValue = 0; if (!byte.TryParse(value, out bValue)) { data = BigEndian.GetBytes( (value.ToLower() == "true")); } else { data = new byte[] { bValue } }; break; } } #endregion packet = packet.Replace(match.Value, Encoding.Default.GetString(data)); } if (packet.StartsWith("{l}") && packet.Length >= 5) { byte[] lengthData = BigEndian.GetBytes(packet.Length - 3); packet = Encoding.Default.GetString(lengthData) + packet.Substring(3); } return(Encoding.Default.GetBytes(packet)); }