public static long Unpack(byte[] content) { if (content.Length == 1 && content[0] <= 0xFF) { return((sbyte)content[0]); } var toInterpret = content.Skip(1).ToArray(); switch (content[0]) { case Markers.Int8: return((sbyte)toInterpret[0]); case Markers.Int16: return(PackStreamBitConverter.ToInt16(toInterpret)); case Markers.Int32: return(PackStreamBitConverter.ToInt32(toInterpret)); case Markers.Int64: return(PackStreamBitConverter.ToInt64(toInterpret)); default: throw new ArgumentOutOfRangeException(nameof(content), content[0], "Unknown Marker"); } }
public static byte[] Pack(double content) { var output = new List <byte> { Markers.Floating }; output.AddRange(PackStreamBitConverter.GetBytes(content)); return(output.ToArray()); }
private static int GetNumberOfFields(byte[] bytes) { if (bytes[0] >= 0xB0 && bytes[0] <= 0xBF) { return(bytes[0] - 0xB0); } if (bytes[0] == 0xDC) { return(bytes[1]); } if (bytes[0] == 0xDD) { var markerSize = bytes.Skip(1).Take(2).ToArray(); return(int.Parse(PackStreamBitConverter.ToString(markerSize).Replace("-", ""), NumberStyles.HexNumber)); } throw new ArgumentOutOfRangeException(nameof(bytes), bytes[0], "Unknown Marker"); }
public static int GetExpectedSize(byte[] content) { if (content[0] >= 0x80 && content[0] <= 0x8F) { return(content[0] - 0x80); } if (content[0] == 0xD0) { return(content[1]); } if (content[0] == 0xD1) { var markerSize = content.Skip(1).Take(2).ToArray(); return((ushort)PackStreamBitConverter.ToInt16(markerSize)); } if (content[0] == 0xD2) { var markerSize = content.Skip(1).Take(4).ToArray(); return(PackStreamBitConverter.ToInt32(markerSize)); } throw new ArgumentOutOfRangeException(nameof(content), content[0], "Unknown Marker"); }
private static int GetNumberOfElements(byte[] content) { if (content[0] >= 0x90 && content[0] <= 0x9F) { return(content[0] - 0x90); } if (content[0] == 0xD4) { return(content[1]); } if (content[0] == 0xD5) { return(int.Parse(PackStreamBitConverter.ToString(content.Skip(1).Take(2).ToArray()).Replace("-", ""), NumberStyles.HexNumber)); } if (content[0] == 0xD6) { return(int.Parse(PackStreamBitConverter.ToString(content.Skip(1).Take(4).ToArray()).Replace("-", ""), NumberStyles.HexNumber)); } throw new ArgumentException("Unknown marker", nameof(content)); }
public static double Unpack(byte[] content) { var markerlessArray = content[0] == Markers.Floating ? content.Skip(1).ToArray() : content; return(PackStreamBitConverter.ToDouble(markerlessArray)); }
internal static byte[] ConvertLongToBytes(long value) { var output = new List <byte>(PackStreamBitConverter.GetBytes(value)); return(value >= 0 ? ConvertPositiveLongs(output, value) : ConvertNegativeLongs(output, value)); }