Example #1
0
        /// <summary>
        /// Convert data to decimal base to specified base
        /// </summary>
        /// <param name="sourceValue">Source base-N string</param>
        /// <param name="baseN">Base-N</param>
        /// <returns></returns>
        public static int ConvertFromBaseN(string sourceValue, int baseN)
        {
            //Validazione argomenti
            if (string.IsNullOrEmpty(sourceValue))
            {
                throw new ArgumentNullException(nameof(sourceValue));
            }
            if (baseN <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(baseN));
            }

            //Definisco i caratteri utilizzati
            const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            //Verifico che la base specificata sia valida
            if (baseN > AllowedChars.Length)
            {
                throw new InvalidProgramException(
                          $"Base-N value must be greater then zero and lower or equals to {AllowedChars.Length}.");
            }

            //Eseguo l'uppercase della stringa
            string elaboratedSource = sourceValue.ToUpper();
            int    decimalValue     = 0;

            //Recupero solo i caratteri contemplati nella base definita
            //quindi separo in caratteri singoli la stringa sorgente
            string baseChars = AllowedChars.Substring(0, baseN);

            char[] allChars = elaboratedSource.ToArray();

            //Scorro tutti i caratteri (partendo dall'ultimo)
            for (int i = 0; i < allChars.Length; i++)
            {
                //Recupero il carattere da elaborare
                var currentChar = allChars[i];

                //Calcolo il peso del carattere all'interno dei caratteri contemplati
                var weight = baseChars.IndexOf(currentChar);

                //Calcolo il moltiplicatore
                var multiplier = (allChars.Length - 1) - i;

                //Calcolo il valore decimale del carattere corrente come
                //posizione dello stesso nella stringa dei caratteri permessi
                //moltiplicato per la base, elevata alla posizione del carattere
                //all'interno della stringa sorgente (es. "100" in binario equivale
                //a 2(base)^2(posizione in stringa) * 1(posizione di "1" in "01")
                double currentDecimalValue = Math.Pow(baseN, multiplier) * weight;

                //Sommo il valore al precedente
                decimalValue = decimalValue + (int)Math.Round(currentDecimalValue, 0);
            }

            //Emetto il decimale
            return(decimalValue);
        }
Example #2
0
        public string CreatePassword(int length)
        {
            const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@$^*()";
            Random       rnd          = new Random();
            string       newPassword  = "";

            int AllowedCharsLen = AllowedChars.Length;

            for (int i = 0; i < length; i++)
            {
                newPassword += AllowedChars.Substring(rnd.Next(0, AllowedCharsLen), 1);
            }

            return(newPassword);
        }