/// <summary> /// Reads an ASCII-string of the specified length from this instance. /// </summary> /// <param name="reader">Extension method for the BinaryReader class.</param> /// <param name="count">The number of bytes to read from the underlying /// stream.</param> /// <returns>A string decoded from the bytes read from the underlying /// stream using the ASCII character set.</returns> public static string ReadASCIIString(this BinaryReader reader, int count) { ByteBuilder builder = new ByteBuilder(); int read = 0; while (true) { if (read++ >= count) break; byte b = reader.ReadByte(); builder.Append(b); } return Encoding.ASCII.GetString(builder.ToArray()).TrimEnd('\0'); }
/// <summary> /// Creates a new "multi-precision integer" from the specified array /// of bytes. /// </summary> /// <param name="data">A big-endian sequence of bytes forming the /// integer value of the multi-precision integer.</param> public Mpi(byte[] data) { byte[] b = new byte[data.Length]; Array.Copy(data.Reverse().ToArray(), b, data.Length); ByteBuilder builder = new ByteBuilder().Append(b); // We append a null byte to the buffer which ensures the most // significant bit will never be set and the big integer value // always be positive. if (b.Last() != 0) builder.Append(0); Value = new BigInteger(builder.ToArray()); }