Esempio n. 1
0
        /// <summary>
        /// Calculates and compares checksum for vat numbers with length between 2 and 10
        /// </summary>
        private bool Checksum(string vat)
        {
            int[] weights    = { 7, 5, 3, 2, 1, 7, 5, 3, 2 };
            int   checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);

            if (vat.Length < 9)
            {
                int length = 9 - vat.Length;

                for (int i = 0; i < length; i++)
                {
                    vat = "0" + vat;
                }
            }

            int sum = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = sum * 10;

            return(checkDigit == Mod(Mod(check, 11), 10));
        }
Esempio n. 2
0
        /// <summary>
        /// Checks the checksum for legal entities, last digit is the check digit.
        /// </summary>
        private bool ChecksumLegal(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);
            int sum = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += (i + 1) * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = Mod(sum, 11);

            if (check == 10)
            {
                sum = 0;

                for (int i = 0; i < vat.Length; i++)
                {
                    sum += (i + 3) * CharUnicodeInfo.GetDigitValue(vat[i]);
                }

                check = Mod(sum, 11);
            }

            return(Mod(check, 10) == checkDigit);
        }
        /// <summary>
        ///     Validates a South African identity number.
        /// </summary>
        /// <param name="identityNumber">13 digit South African identity number, defined as YYMMDDSSSSCAZ.</param>
        /// <returns>
        ///     Either either true or false.
        /// </returns>
        public static bool IsValidSouthAfricanIdentityNumber(this string identityNumber)
        {
            if (string.IsNullOrWhiteSpace(value: identityNumber))
            {
                return(false);
            }

            if (new string(value : Array.FindAll(array : identityNumber.ToArray(), match : c => char.IsDigit(c : c)))
                .Length != 13)
            {
                return(false);
            }

            DateTime dateOfBirth;

            if (!DateTime.TryParse(
                    s: $"19{identityNumber.Substring(startIndex: 0, length: 2)}-{identityNumber.Substring(startIndex: 2, length: 2)}-{identityNumber.Substring(startIndex: 4, length: 2)}",
                    provider: CultureInfo.InvariantCulture,
                    styles: DateTimeStyles.AllowWhiteSpaces, result: out dateOfBirth))
            {
                return(false);
            }

            var identityNumberChecksumDigit =
                CharUnicodeInfo.GetDigitValue(ch: identityNumber[index: identityNumber.Length - 1]);
            var identityNumberSection = identityNumber.Substring(startIndex: 0, length: identityNumber.Length - 1);

            return(identityNumberChecksumDigit ==
                   CalculateLuhnChecksumDigit(identityNumberSection: identityNumberSection));
        }
Esempio n. 4
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Attempts to parse the given token as a layout position string.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns>The enumeration corresponding to the parsed token, or
        /// PictureLayoutPosition.CenterInColumn if unable to parse.</returns>
        /// ------------------------------------------------------------------------------------
        private static int ParseScaleFactor(string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return(100);
            }

            int scaleFactor = 0;

            foreach (char ch in token)
            {
                int value = CharUnicodeInfo.GetDigitValue(ch);
                if (value >= 0 && scaleFactor <= 100)
                {
                    scaleFactor = (scaleFactor == 0) ? value : scaleFactor * 10 + value;
                }
                else if (scaleFactor > 0)
                {
                    break;
                }
            }
            if (scaleFactor == 0)
            {
                if (!String.IsNullOrEmpty(token))
                {
                    Logger.WriteEvent("Unexpected CmPicture Scale value: " + token);
                }
                scaleFactor = 100;
            }
            else
            {
                scaleFactor = Math.Min(scaleFactor, 1000);
            }
            return(scaleFactor);
        }
Esempio n. 5
0
 public static void PrintProperties(char c)
 {
     Console.Write(" {0,-3}", c);
     Console.Write(" {0,-5}", CharUnicodeInfo.GetNumericValue(c));
     Console.Write(" {0,-5}", CharUnicodeInfo.GetDigitValue(c));
     Console.Write(" {0,-5}", CharUnicodeInfo.GetDecimalDigitValue(c));
     Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(c));
 }
Esempio n. 6
0
 public static void DigitsTest()
 {
     Assert.Equal(s_numericNonDecimalCodepoints.Length, s_numericNonDecimalValues.Length);
     for (int i = 0; i < s_numericNonDecimalCodepoints.Length; i++)
     {
         Assert.Equal(s_numericNonDecimalValues[i], CharUnicodeInfo.GetDigitValue(s_numericNonDecimalCodepoints[i]));
         Assert.Equal(s_numericNonDecimalValues[i], CharUnicodeInfo.GetDigitValue(s_numericNonDecimalCodepoints, i));
     }
 }
Esempio n. 7
0
        public static int[] GetDigits(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return new int[] { }
            }
            ;

            return(str.ToCharArray().Where(x => char.IsDigit(x)).Select(x => CharUnicodeInfo.GetDigitValue(x)).ToArray());
        }
Esempio n. 8
0
        // Sum of Digits / Digital Root
        public static int DigitalRoot(long n)
        {
            if (n.ToString().Length == 1)
            {
                return((int)n);
            }
            int sum = n.ToString().Select(p => CharUnicodeInfo.GetDigitValue(p)).Sum();

            return(DigitalRoot(sum));
        }
Esempio n. 9
0
        // Persistent Bugger
        public static int Persistence(long n)
        {
            int counter = 0;

            while (n > 9)
            {
                n = n.ToString().Select(p => CharUnicodeInfo.GetDigitValue(p)).Aggregate((x, y) => x * y);
                counter++;
            }
            return(counter);
        }
Esempio n. 10
0
        /// <summary>
        /// Calculates and compares checksum for legal entities
        /// </summary>
        private bool LegalChecksum(string vat)
        {
            int[] weights = { 9, 1, 4, 8, 3, 10, 2, 5, 7, 6, 1 };
            int   sum     = 0;

            for (int i = 0; i < weights.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            return(Mod(sum, 11) == 3);
        }
Esempio n. 11
0
        public void GetDigitValue_Char()
        {
            for (int i = 0; i <= char.MaxValue; i++)
            {
                char ch = (char)i;

                CodePoint knownGoodData = UnicodeData.GetData(ch);
                int       actualValue   = CharUnicodeInfo.GetDigitValue(ch);

                AssertEqual(knownGoodData.DigitValue, actualValue, nameof(CharUnicodeInfo.GetDigitValue), knownGoodData);
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Calculates and compares chcecksum
        /// </summary>
        private bool Checksum(string vat)
        {
            int[] weights = { 9, 7, 3, 1, 9, 7, 3, 1 };
            int   sum     = 0;

            for (int i = 0; i < 8; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            return(Mod(sum, 10) == 0);
        }
Esempio n. 13
0
        /// <summary>
        /// Calculates and compares checksum
        /// </summary>
        private bool Checksum(string vat)
        {
            int[] weights = { 6, 5, 7, 2, 3, 4, 5, 6, 7, -1 };
            int   sum     = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            return(Mod(sum, 11) == 0);
        }
        /// <summary>
        ///     Maps <paramref name="identityNumber" /> to a
        ///     <see cref="StatementBruteForce.Core.SouthAfricanIdentityNumberModel" /> object.
        /// </summary>
        /// <param name="identityNumber">13 digit South African identity number, defined as YYMMDDSSSSCAZ.</param>
        /// <returns>
        ///     A <see cref="StatementBruteForce.Core.SouthAfricanIdentityNumberModel" /> object.
        /// </returns>
        public static SouthAfricanIdentityNumberModel ParseIdentityNumberStringToModel(string identityNumber)
        {
            var chars = identityNumber.ToCharArray();

            #region local functions

            int yy()
            {
                if (char.IsDigit(c: chars[0]) && char.IsDigit(c: chars[1]))
                {
                    return(1900 + 10 * CharUnicodeInfo.GetDigitValue(ch: chars[0]) +
                           CharUnicodeInfo.GetDigitValue(ch: chars[1]));
                }

                return(-1);
            }

            int mm()
            {
                if (char.IsDigit(c: chars[2]) && char.IsDigit(c: chars[3]))
                {
                    return(10 * CharUnicodeInfo.GetDigitValue(ch: chars[2]) +
                           CharUnicodeInfo.GetDigitValue(ch: chars[3]));
                }

                return(-1);
            }

            int dd()
            {
                if (char.IsDigit(c: chars[4]) && char.IsDigit(c: chars[5]))
                {
                    return(10 * CharUnicodeInfo.GetDigitValue(ch: chars[4]) +
                           CharUnicodeInfo.GetDigitValue(ch: chars[5]));
                }

                return(-1);
            }

            #endregion


            var model = new SouthAfricanIdentityNumberModel(yearOfBirth: yy(), monthOfBirth: mm(), dayOfBirth: dd(),
                                                            gender: char.IsDigit(c: chars[6]) ? CharUnicodeInfo.GetDigitValue(ch: chars[6]) : -1,
                                                            genderSequence1: char.IsDigit(c: chars[7]) ? CharUnicodeInfo.GetDigitValue(ch: chars[7]) : -1,
                                                            genderSequence2: char.IsDigit(c: chars[8]) ? CharUnicodeInfo.GetDigitValue(ch: chars[8]) : -1,
                                                            genderSequence3: char.IsDigit(c: chars[9]) ? CharUnicodeInfo.GetDigitValue(ch: chars[9]) : -1,
                                                            citizenship: char.IsDigit(c: chars[10]) ? CharUnicodeInfo.GetDigitValue(ch: chars[10]) : -1,
                                                            obsolete: char.IsDigit(c: chars[11]) ? CharUnicodeInfo.GetDigitValue(ch: chars[11]) : -1,
                                                            checksum: char.IsDigit(c: chars[12]) ? CharUnicodeInfo.GetDigitValue(ch: chars[12]) : -1);
            return(model);
        }
Esempio n. 15
0
        /// <summary>
        /// 获取Unicode简体字符集合
        /// </summary>
        /// <returns></returns>
        unsafe static string get()
        {
            string simplified = new string((char)0, 65536);

            fixed(char *simplifiedFixed = simplified)
            {
                char *end = simplifiedFixed + 65536;

                for (char code = (char)65535; end != simplifiedFixed; *--end = code--)
                {
                    ;
                }
                simplified = Strings.StrConv(simplified, VbStrConv.SimplifiedChinese, 0).ToLower();
            }

            fixed(char *simplifiedFixed = simplified)
            {
                char *end = simplifiedFixed + 65536;

                for (char code = (char)65535; end != simplifiedFixed; --code)
                {
                    --end;
                    UnicodeCategory category = CharUnicodeInfo.GetUnicodeCategory(code);
                    if ((code >= 0x4E00 && code <= 0X9FA5) ||
                        category == UnicodeCategory.LowercaseLetter || category == UnicodeCategory.UppercaseLetter ||
                        category == UnicodeCategory.TitlecaseLetter || category == UnicodeCategory.ModifierLetter ||
                        category == UnicodeCategory.OtherLetter || category == UnicodeCategory.DecimalDigitNumber ||
                        category == UnicodeCategory.LetterNumber || category == UnicodeCategory.OtherNumber ||
                        code == '&' || code == '.' || code == '+' || code == '#')
                    {
                        if (code > 65280 && code < 65375)
                        {
                            *end = (char)(code - 65248);
                        }
                        else if (category == UnicodeCategory.DecimalDigitNumber)
                        {
                            *end = (char)(48 + CharUnicodeInfo.GetDigitValue(code));
                        }
                        else if (*end == '?')
                        {
                            *end = code;
                        }
                    }
                    else
                    {
                        *end = ' ';
                    }
                }
                return(simplified);
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Calculates and compares checksum
        /// </summary>
        private bool Checksum(string vat)
        {
            vat = vat.Substring(0, 9);
            int sum = 0;

            for (int i = 0; i < vat.Length - 1; i++)
            {
                sum += (9 - i) * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = sum - CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            return(Mod(check, 11) == 0);
        }
Esempio n. 17
0
        /// <summary>
        /// Calculates and comapres checksum with 0
        /// </summary>
        private bool Checksum(string vat)
        {
            int[] weights = { 7, 9, 10, 5, 8, 4, 2, 1 };
            int   sum     = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = Mod(sum, 11);

            return(check == 0);
        }
Esempio n. 18
0
        /// <summary>
        /// Calculates and compares checksum
        /// </summary>
        private bool Checksum(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);
            int sum = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += (9 - i) * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            return(checkDigit == Mod(Mod(11 - sum, 11), 10));
        }
Esempio n. 19
0
        /// <summary>
        /// Calculates and compare checksum with check
        /// </summary>
        private bool Checksum(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);
            int check = 0;

            foreach (char c in vat)
            {
                check = check * 2 + CharUnicodeInfo.GetDigitValue(c);
            }

            return(checkDigit == Mod(Mod(check * 2, 11), 10));
        }
Esempio n. 20
0
        public override string Validate(string vat)
        {
            if (vat.Length != 11)
            {
                throw new InvalidLengthException();
            }

            if (CharUnicodeInfo.GetDigitValue(vat[0]) > 3 && !LegalChecksum(vat) && !ValidateDate(vat) && !IndividualChecksum(vat))
            {
                throw new InvalidChecksumException();
            }

            return(vat);
        }
Esempio n. 21
0
        /// <summary>
        /// Calculates and compares checksum for other
        /// </summary>
        private bool Checksum(string vat)
        {
            if (vat[0] == 'K' || vat[0] == 'L' || vat[0] == 'M')
            {
                return(IndividualChecksum(vat.Substring(1)));
            }

            char checkChar = vat[vat.Length - 1];

            vat = vat.Substring(0, vat.Length - 1);
            string alphabet = "0123456789";
            char   check    = alphabet[10 - LuhnChecksum(vat.Substring(1) + "0")];

            return((check.ToString() + "JABCDEFGHI"[CharUnicodeInfo.GetDigitValue(check)]).Contains(checkChar.ToString()));
        }
Esempio n. 22
0
        /// <summary>
        /// Calculates and compares checksum with check for others
        /// </summary>
        private bool Checksum(string vat)
        {
            vat = vat.Substring(0, 9);
            int[] weights = { 8, 7, 6, 5, 4, 3, 2, 10, 1 };
            int   sum     = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = Mod(sum, 97);

            return(check == 0 || check == 42 || check == 55);
        }
Esempio n. 23
0
        /// <summary>
        /// Calculates the checksum and compares it with check
        /// </summary>
        private bool Checksum(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);

            int result = 6 - Mod(LuhnChecksum(vat), 10);

            if (result < 0)
            {
                result += 10;
            }

            return(result == checkDigit);
        }
Esempio n. 24
0
        protected int Mod1110(string number)
        {
            int check = 5;

            foreach (char n in number)
            {
                if (check == 0)
                {
                    check = 10;
                }

                check = (Mod(check * 2, 11) + CharUnicodeInfo.GetDigitValue(n)) % 10;
            }

            return(check);
        }
Esempio n. 25
0
        /// <summary>
        /// Validates the check digit for foreigner personal number
        /// </summary>
        private bool Pnf(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);
            int[] weights = { 21, 19, 17, 13, 11, 9, 7, 3, 1 };
            int   sum     = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = Mod(sum, 10);

            return(check == checkDigit);
        }
Esempio n. 26
0
        /// <summary>
        /// Validates the check digit for others - individuals, foreigners, ...
        /// </summary>
        private bool ChecksumOther(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);
            int[] weights = { 4, 3, 2, 7, 6, 5, 4, 3, 2 };
            int   sum     = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = Mod(11 - sum, 11);

            return(check == checkDigit);
        }
Esempio n. 27
0
        /// <summary>
        /// Compares the check digit for special cases
        /// </summary>
        private bool ChecksumSpecial(string vat)
        {
            int checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(1, vat.Length - 2);

            int sum = 0;

            for (int i = 0; i < vat.Length; i++)
            {
                sum += (8 - i) * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            sum = Mod(sum, 11);

            return(8 - Mod(Mod(10 - sum, 11), 10) == checkDigit);
        }
Esempio n. 28
0
        /// <summary>
        /// Calculates and compares checksum for individuals
        /// </summary>
        private bool IndividualChecksum(string vat)
        {
            int[] weights    = { 10, 5, 8, 4, 2, 1, 6, 3, 7, 9 };
            int   checkDigit = CharUnicodeInfo.GetDigitValue(vat[vat.Length - 1]);

            vat = vat.Substring(0, vat.Length - 1);
            int sum = 0;

            for (int i = 0; i < weights.Length; i++)
            {
                sum += weights[i] * CharUnicodeInfo.GetDigitValue(vat[i]);
            }

            int check = 1 + sum;

            return(Mod(Mod(check, 11), 10) == checkDigit);
        }
Esempio n. 29
0
        private bool ValidateDate(string vat)
        {
            int day   = Convert.ToInt32(vat.Substring(0, 2));
            int month = Convert.ToInt32(vat.Substring(2, 2));
            int year  = Convert.ToInt32(vat.Substring(4, 2));

            year += 1800 + CharUnicodeInfo.GetDigitValue(vat[6]) * 100;

            try
            {
                DateTime dateTime = new DateTime(year, month, day);
                return(dateTime <= DateTime.Now);
            }
            catch (Exception)
            {
                return(false);
            }
        }
    public static void Main()
    {
        // The String to get information for.
        String s = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788";

        Console.WriteLine("String: {0}", s);

        // Print the values for each of the characters in the string.
        Console.WriteLine("index c  Num   Dig   Dec   UnicodeCategory");
        for (int i = 0; i < s.Length; i++)
        {
            Console.Write("{0,-5} {1,-3}", i, s[i]);
            Console.Write(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i));
            Console.Write(" {0,-5}", CharUnicodeInfo.GetDigitValue(s, i));
            Console.Write(" {0,-5}", CharUnicodeInfo.GetDecimalDigitValue(s, i));
            Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i));
        }
    }