Ejemplo n.º 1
0
        public static string FromBigInteger(this NumeralSystem system, BigInteger value, int fixedLength = 0, int groupSize = 0, string groupSeparator = " ")
        {
            var chars     = new char[20];
            var charcount = 0;

            do
            {
                chars[chars.Length - charcount - 1] = system.GetSymbolFor((int)(value % system.Base));
                value = value / system.Base;
                charcount++;
                if (charcount >= chars.Length)
                {
                    var oldchars = chars;
                    chars = new char[oldchars.Length + 30];
                    Array.Copy(oldchars, 0, chars, 30, oldchars.Length);
                }
            } while (value > 0);

            var result = new String(chars, chars.Length - charcount, charcount);

            if (result.Length < fixedLength)
            {
                result = new String(system.GetSymbolFor(0), fixedLength - result.Length) + result;
            }
            if (result.Length > groupSize && groupSize > 0 && !String.IsNullOrEmpty(groupSeparator))
            {
                return(String.Join(groupSeparator, result.Chunked(groupSize, true)));
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static BigInteger ParseBigInteger(this NumeralSystem system, string s)
        {
            s = system.PrepareForParse(s);

            var result = new BigInteger();

            foreach (var c in s)
            {
                var value = system.GetValueForSymbol(c);
                if (value == -1)
                {
                    continue;
                }
                result = (result * system.Base) + value;
            }
            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Converts a value expressed in the current numeral system into another numeral system. Can handle values of any size.
 /// </summary>
 /// <param name="targetNumeralSystem">The numeral system to convert to.</param>
 /// <param name="value">The value expressed in the current numeral system.</param>
 /// <param name="fixedLength">If given, result will have at least the given length, padded with '0' symbols on the left.</param>
 /// <param name="groupSize">To group symbols together, give a groupSize.</param>
 /// <param name="groupSeparator">String to separate groups.</param>
 /// <returns>The value converted to the target numeral system.</returns>
 public string ConvertBigTo(NumeralSystem targetNumeralSystem, string value, int fixedLength = 0, int groupSize = 0, string groupSeparator = " ")
 {
     return(targetNumeralSystem.FromBigInteger(this.ParseBigInteger(value), fixedLength, groupSize, groupSeparator));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a numeral value.
 /// </summary>
 public NumeralValue(NumeralSystem system, string value)
 {
     this.Value  = value;
     this.System = system;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates a numeral value.
 /// </summary>
 public NumeralValue(NumeralSystem system, long value)
     : this(system, system.From(value))
 {
 }