/// <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> /// Given a <see cref="NumeralString"/> and alphabet, ensure the values within the <see cref="NumeralString"/> are valid given the alphabet. /// </summary> /// <param name="alphabet"></param> /// <param name="numeralString"></param> /// <returns></returns> public static bool IsNumeralStringValidWithAlphabet(string alphabet, NumeralString numeralString) { if (!IsAlphabetValid(alphabet)) { return(false); } var maxValueFromNumeralString = numeralString.Numbers.Max(); if (maxValueFromNumeralString > alphabet.Length) { return(false); } return(true); }
/// <summary> /// Converts the provided <see cref="NumeralString"/> into its alphabet representation. /// </summary> /// <param name="alphabet">The alphabet to use for the conversion process.</param> /// <param name="radix">The base.</param> /// <param name="numeralStringToConvert">The <see cref="NumeralString"/> to convert to its alphabet representation.</param> /// <returns>The string that represents the <see cref="NumeralString"/> converted to it's alphabet.</returns> public static string ToAlphabetString(string alphabet, int radix, NumeralString numeralStringToConvert) { if (!IsNumeralStringValidWithAlphabet(alphabet, numeralStringToConvert)) { return(null); } if (alphabet.Length != radix) { return(null); } var sb = new StringBuilder(); foreach (var number in numeralStringToConvert.Numbers) { sb.Append(alphabet[number]); } return(sb.ToString()); }