/// <summary> /// Converts a provided <see cref="NumeralString"/> to a <see cref="BitString"/>. /// /// Numerals within the <see cref="NumeralString"/> are int (16 bits) and are converted /// to bits and concatenated onto a <see cref="BitString"/> then returned. /// </summary> /// <param name="numeralString">The <see cref="NumeralString"/> to convert.</param> /// <returns>The <see cref="BitString"/> representation of a <see cref="NumeralString"/>.</returns> public static BitString ToBitString(NumeralString numeralString) { var bs = new BitString(0); foreach (var number in numeralString.Numbers) { bs = bs.ConcatenateBits(BitString.To32BitString(number).GetLeastSignificantBits(16)); } return(bs); }
/// <summary> /// Takes a BitString and adds LSBs (or MSBs when <see cref="padOntoLsb"/> is false) to make the BitString hit (BitString % modulus = 0) /// </summary> /// <param name="bs">The BitString to pad</param> /// <param name="modulus">The modulus to pad the bitstring such that BitString % modulusToHit = 0</param> /// <param name="padOntoLsb">When true bits are added on the least significant end, the most significant end otherwise.</param> /// <returns>The padded BitString</returns> public static BitString PadToModulus(BitString bs, int modulus, bool padOntoLsb = true) { if (bs.BitLength % modulus == 0) { return(bs); } var bitsToAdd = (modulus - bs.BitLength % modulus); return(padOntoLsb ? bs.ConcatenateBits(new BitString(bitsToAdd)) : new BitString(bitsToAdd).ConcatenateBits(bs)); }
/// <summary> /// Only for use in SHA3 /// </summary> /// <returns>Normal hex, but the last byte is truncated to match little endian format</returns> public string ToLittleEndianHex() { if (BitLength == 0) { return("00"); } var bytes = new byte[] { }; // Make a padded BitString if the length isn't % 8 if (BitLength % BITSINBYTE != 0) { var padding = BITSINBYTE - BitLength % BITSINBYTE; var extraBits = BitLength % BITSINBYTE; var lastBits = this.Substring(0, extraBits); var paddedBS = new BitString(0); if (BitLength < BITSINBYTE) { paddedBS = BitString.Zeroes(padding); paddedBS = BitString.ConcatenateBits(paddedBS, lastBits); } else { var firstBits = this.Substring(extraBits, BitLength - extraBits); paddedBS = BitString.ConcatenateBits(paddedBS, firstBits); paddedBS = BitString.ConcatenateBits(paddedBS, BitString.Zeroes(padding)); paddedBS = BitString.ConcatenateBits(paddedBS, lastBits); } bytes = paddedBS.ToBytes(); } else { bytes = ToBytes(); } StringBuilder hex = new StringBuilder(bytes.Length * 2); for (int index = 0; index < bytes.Length; index++) { hex.AppendFormat("{0:x2}", bytes[index]); } return(hex.ToString().ToUpper()); }
public BitString ConcatenateBits(BitString bitsToAppend) { return(BitString.ConcatenateBits(this, bitsToAppend)); }