public static int DecodeUInt(byte[] buf, int offset, int endBoundary, ref uint output) { var bytes = buf.Skip(offset).Take(endBoundary - offset).ToArray(); output = VarintBitConverter.ToUInt32(bytes); return(VarintBitConverter.GetVarintBytes(output).Length); }
public static int GetBytesLength <T>(T value, Encoding encoding = null) { var type = typeof(T); if (type.IsValueType) { if (value is short) { short data = Convert.ToInt16(value); return(VarintBitConverter.GetVarintBytes(data).Length); } else if (value is int) { int data = Convert.ToInt32(value); return(VarintBitConverter.GetVarintBytes(data).Length); } else if (value is long) { long data = Convert.ToInt64(value); return(VarintBitConverter.GetVarintBytes(data).Length); } else { ulong data = Convert.ToUInt64(value); return(data < 128 ? 1 : data < 16384 ? 2 : data < 2097152 ? 3 : data < 268435456 ? 4 : data < 34359738368 ? 5 : data < 4398046511104 ? 6 : data < 562949953421312 ? 7 : data < 72057594037927936 ? 8 : data < 9223372036854775808 ? 9 : 10); } } if (type == typeof(string)) { if (encoding == null) { encoding = Encoding.Default; } int count = encoding.GetByteCount(Convert.ToString(value)); return(VarintBitConverter.GetBytesLength(count)); } if (type == typeof(byte[])) { var bytes = value as byte[]; int count = bytes.Length; return(VarintBitConverter.GetBytesLength(count)); } throw new TypeAccessException("'" + type.FullName + "' cannot be converted to VarInt"); }
public static int EncodeUInt(uint value, ref byte[] output) { output = VarintBitConverter.GetVarintBytes(value); return(output.Length); }