public void Write(BigInteger value) { // Check to determine if long type is able // to be packed inside an Int32 or is actually // an Int64 value. bool isLong = (value > int.MaxValue || value < int.MinValue); if (!isLong && value >= 0 && value < RencodeConst.EncodedPositiveIntegerUpperBound) { stream.WriteByte((byte)((int)RencodeTypeCode.EncodedPositiveInteger + value)); } else if (!isLong && value >= -RencodeConst.EncodedNegativeIntegerUpperBound && value < 0) { stream.WriteByte((byte)((int)RencodeTypeCode.EncodedNegativeInteger - 1 - value)); } else if (!isLong && value >= sbyte.MinValue && value < sbyte.MaxValue) { stream.WriteByte((byte)RencodeTypeCode.SByte); stream.WriteByte((byte)(sbyte)value); } else if (!isLong && value >= short.MinValue && value < short.MaxValue) { stream.WriteByte((byte)RencodeTypeCode.Int16); byte[] bytes = BStruct.Pack((short)value); stream.Write(bytes, 0, bytes.Length); } else if (value >= int.MinValue && value < int.MaxValue) { stream.WriteByte((byte)RencodeTypeCode.Int32); byte[] bytes = BStruct.Pack((int)value); stream.Write(bytes, 0, bytes.Length); } else if (value >= long.MinValue && value <= long.MaxValue) { stream.WriteByte((byte)RencodeTypeCode.Int64); byte[] bytes = BStruct.Pack((long)value); stream.Write(bytes, 0, bytes.Length); } else { string s = value.ToString(System.Globalization.CultureInfo.InvariantCulture); if (s.Length >= RencodeConst.MaxBase10IntegerStringLength) { throw new ArgumentOutOfRangeException(); } stream.WriteByte((byte)RencodeTypeCode.BigInteger); byte[] encoded = Util.GetBytes(s); stream.Write(encoded, 0, encoded.Length); stream.WriteByte((byte)RencodeTypeCode.Termination); } }
private static void EncodeInt(object x, List <object> dest) { // Check to determine if long type is able // to be packed inside an Int32 or is actually // an Int64 value. bool isLong = x is long && (((long)x) > int.MaxValue || ((long)x) < int.MinValue); if (!isLong && 0 <= (int)x && (int)x < RencodeConst.INT_POS_FIXED_COUNT) { dest.Add((char)(RencodeConst.INT_POS_FIXED_START + (int)x)); } else if (!isLong && -RencodeConst.INT_NEG_FIXED_COUNT <= (int)x && (int)x < 0) { dest.Add((char)(RencodeConst.INT_NEG_FIXED_START - 1 - (int)x)); } else if (!isLong && -128 <= (int)x && (int)x < 128) { dest.Add(RencodeConst.CHR_INT1); dest.Add(BStruct.Pack(x, 1)); } else if (!isLong && -32768 <= (int)x && (int)x < 32768) { dest.Add(RencodeConst.CHR_INT2); dest.Add(BStruct.Pack(x, 2)); } else if (-2147483648L <= Convert.ToInt64(x) && Convert.ToInt64(x) < 2147483648L) { dest.Add(RencodeConst.CHR_INT4); dest.Add(BStruct.Pack(x, 4)); } else if (-9223372036854775808L < Convert.ToInt64(x) && Convert.ToInt64(x) < 9223372036854775807L) { dest.Add(RencodeConst.CHR_INT8); dest.Add(BStruct.Pack(x, 8)); } else { string s = (string)x; if (s.Length >= RencodeConst.MAX_INT_LENGTH) { throw new ArgumentOutOfRangeException(); } dest.Add(RencodeConst.CHR_INT); dest.Add(s); dest.Add(RencodeConst.CHR_TERM); } }
public void Write(double value) { stream.WriteByte((byte)RencodeTypeCode.Double); byte[] bytes = BStruct.Pack(value); stream.Write(bytes, 0, bytes.Length); }
public static void Write(this MemoryStream stream, double x) => stream.Write(BStruct.Pack(x));
private static void EncodeDouble(object x, List <object> dest) { dest.Add(RencodeConst.CHR_FLOAT64); dest.Add(BStruct.Pack(x, 8)); }
private static void EncodeFloat(object x, List <object> dest) { dest.Add(RencodeConst.CHR_FLOAT32); dest.Add(BStruct.Pack(x, 4)); }