private int GetISBNTypeLen(ISBNTypeEnum ISBNType)
        {
            int functionReturnValue = 0;

            switch (ISBNType)
            {
            case ISBNTypeEnum.isISBN10:
                functionReturnValue = 10;
                break;

            case ISBNTypeEnum.isISBN13:
                functionReturnValue = 13;
                break;

            case ISBNTypeEnum.isISSN:
                functionReturnValue = 8;
                break;

            case ISBNTypeEnum.isISMN:
                functionReturnValue = 10;
                break;

            case ISBNTypeEnum.isEAN:
                functionReturnValue = 13;
                break;
            }
            return(functionReturnValue);
        }
        private ISBNTypeEnum GetISBNType(string sNum)
        {
            ISBNTypeEnum functionReturnValue = default(ISBNTypeEnum);
            int          l = Strings.Len(sNum);
            string       c = Strings.Left(sNum, 1);

            if (l == 9 || l == 10)
            {
                functionReturnValue = ISBNTypeEnum.isISBN10; return(functionReturnValue);
            }
            if (l == 7 || l == 8)
            {
                functionReturnValue = ISBNTypeEnum.isISSN; return(functionReturnValue);
            }
            if (l == 12 || l == 13)
            {
                switch (sNum.Substring(0, 3))
                {
                case "978":
                case "979":
                    functionReturnValue = ISBNTypeEnum.isISBN13;
                    return(functionReturnValue);

                default:
                    functionReturnValue = ISBNTypeEnum.isEAN;
                    return(functionReturnValue);
                }
            }
            if (c == "M")
            {
                functionReturnValue = ISBNTypeEnum.isISMN; return(functionReturnValue);
            }
            return(functionReturnValue);
        }
        private bool Valid(string sNum, ISBNTypeEnum iType)
        {
            bool   v = Convert.ToBoolean(iType < ISBNTypeEnum.isFinalEntry);
            int    x = 0;
            string c = null;

            bool continueLoop = true;

            for (x = 1; x <= GetISBNTypeLen(iType) && continueLoop; x++)
            {
                c = Strings.UCase(Strings.Mid(sNum, x, 1));
                if ((x > 1 || c != "M" || iType != ISBNTypeEnum.isISSN) && (c.CompareTo("0") < 0 || c.CompareTo("9") < 0))
                {
                    if (x == GetISBNTypeLen((ISBNTypeEnum)iType) && c == "X")
                    {
                    }
                    else
                    {
                        v            = false;
                        continueLoop = false;
                    }
                }
            }
            return(v);
        }
Example #4
0
        private int GetISBNTypeLen(ISBNTypeEnum ISBNType)
        {
            switch (ISBNType)
            {
            case ISBN.ISBNTypeEnum.isISBN10: return(10);

            case ISBN.ISBNTypeEnum.isISBN13: return(13);

            case ISBN.ISBNTypeEnum.isISSN: return(8);

            case ISBN.ISBNTypeEnum.isISMN: return(10);

            case ISBN.ISBNTypeEnum.isEAN: return(13);

            default: throw new ArgumentException(String.Format("Unexpected ISBNType {0}", ISBNType));
            }
        }
        private string GetCheckDigit(string sNum)
        {
            string functionReturnValue = null;

            functionReturnValue = clsSupport.bpeNullString;
            int          x          = 0;
            int          Weight     = 0;
            int          CheckSum   = 0;
            string       CheckDigit = null;
            ISBNTypeEnum ISBNType   = GetISBNType(sNum);

            switch (ISBNType)
            {
            case ISBNTypeEnum.isISBN10:
            case ISBNTypeEnum.isISSN:
                //ISBN    0  8  7  7  7   9  5  0  5  3
                //Weight 10  9  8  7  6   5  4  3  2
                //        0+72+56+49+42+ 45+20+ 0+10+3
                //For x = 1 To TypeLen(t) - 1
                //    CheckSum = CheckSum + (TypeLen(t) - x + 1) * Mid(sNum, x, 1)
                //Next x
                //CheckDigit = CStr((1100 - CheckSum) Mod 11)
                //If CheckDigit = 10 Then CheckDigit = "X"
                Weight = 2;
                for (int i = GetISBNTypeLen(ISBNType) - 1; i >= 1; i += -1)
                {
                    CheckSum += Convert.ToInt32(sNum.Substring(i - 1, 1)) * Weight;
                    Weight   += 1;
                }

                CheckSum = CheckSum % 11;
                switch (CheckSum)
                {
                case 0:
                    CheckDigit = "0";
                    break;

                case 1:
                    CheckDigit = "X";
                    break;

                default:
                    CheckDigit = Convert.ToString(11 - CheckSum);
                    break;
                }
                break;

            case ISBNTypeEnum.isISBN13:
                for (int i = GetISBNTypeLen(ISBNType) - 1; i >= 1; i += -1)
                {
                    CheckSum += Convert.ToInt32(sNum.Substring(i - 1, 1)) * ((i % 2) == 0 ? 3 : 1);
                }

                CheckSum = CheckSum % 10;
                switch (CheckSum)
                {
                case 0:
                    CheckDigit = "0";
                    break;

                default:
                    CheckDigit = Convert.ToString(10 - CheckSum);
                    break;
                }
                break;

            default:
                if (ISBNType == ISBNTypeEnum.isISMN)
                {
                    CheckSum = 9;
                }
                for (x = 3 - (int)ISBNType; x <= GetISBNTypeLen(ISBNType); x++)
                {
                    CheckSum = CheckSum + (3 - 2 * ((x + (int)ISBNType) % 2)) * Convert.ToInt32(Strings.Mid(sNum, x + 1, 1));
                }

                CheckDigit = ((1000 - CheckSum) % 10).ToString();
                break;
            }
            functionReturnValue = CheckDigit;
            return(functionReturnValue);
        }
        public string CheckISBN(string strISBN)
        {
            string functionReturnValue = null;

            functionReturnValue = clsSupport.bpeNullString;
            string       ISBN       = getISBN(strISBN);
            ISBNTypeEnum iType      = GetISBNType(ISBN);
            string       CheckDigit = null;
            string       cd         = null;
            string       Message    = null;
            string       EAN        = null;

            if (Valid(ISBN, iType))
            {
                CheckDigit = GetCheckDigit(ISBN);
                Message    = "This is an " + Information.TypeName(iType) + ".  The check digit is ";
                if (Strings.Len(ISBN) == GetISBNTypeLen(iType))
                {
                    cd = CheckDigit;
                    if (!ISBN.EndsWith(cd))
                    {
                        Message = Message + "incorrect: it should be " + CheckDigit + ".";
                        Interaction.MsgBox("Incorrect check digit: " + Strings.Right(ISBN, 1) + " entered, but should be " + cd + ".", Constants.vbExclamation, "checkISBN");
                    }
                    else
                    {
                        Message = Message + "correct.";
                    }
                }
                else
                {
                    Message = Message + CheckDigit + ".";
                }
                Message = Message + Constants.vbCrLf;
                Message = Message + "The full " + Information.TypeName(iType) + " is " + FullNum(ISBN) + "." + Constants.vbCrLf;
                if (iType == ISBNTypeEnum.isEAN && Strings.Mid(ISBN, 1, 3) == "977")
                {
                    Message = Message + "It is for a serial publication with ISSN " + FullNum(Strings.Mid(ISBN, 4, 10)) + ".";
                }
                if (iType == ISBNTypeEnum.isEAN && Strings.Mid(ISBN, 1, 4) == "9790")
                {
                    Message = Message + "It is for a piece of music with ISMN " + FullNum("M" + Strings.Mid(ISBN, 5, 13)) + ".";
                }
                functionReturnValue = FullNum(ISBN);
            }
            else
            {
                if (Strings.Len(ISBN) > 13 && Strings.Mid(ISBN, 1, 3) == "977" && Valid(Strings.Mid(ISBN, 1, 13), ISBNTypeEnum.isEAN))
                {
                    EAN = Strings.Mid(ISBN, 1, 13);
                    if (EAN == FullNum(EAN))
                    {
                        Message             = EAN + " is an EAN. The check digit is correct." + Constants.vbCrLf;
                        Message             = Message + "It is for a serial publication with ISSN " + FullNum(Strings.Mid(ISBN, 4, 10)) + ", issue number " + Strings.Mid(ISBN, 14, Strings.Len(ISBN)) + ".";
                        functionReturnValue = FullNum(Strings.Mid(ISBN, 4, 10));
                    }
                    else
                    {
                        Message = "You have typed in too much, too little, or letters instead of numbers.";
                        Interaction.MsgBox(Message, Constants.vbExclamation, "checkISBN");
                        functionReturnValue = strISBN;
                    }
                }
                else
                {
                    Message = "You have typed in too much, too little, or letters instead of numbers.";
                    Interaction.MsgBox(Message, Constants.vbExclamation, "checkISBN");
                    functionReturnValue = strISBN;
                }
            }
            return(functionReturnValue);
            //checkISBN = Message
        }
Example #7
0
        public string CheckISBN(string strISBN)
        {
            string       ISBN       = GetISBN(strISBN);
            ISBNTypeEnum iType      = GetISBNType(ISBN);
            string       CheckDigit = null;
            string       cd         = null;
            string       Message    = null;
            string       EAN        = null;

            if (Valid(ISBN, iType))
            {
                CheckDigit = GetCheckDigit(ISBN);
                Message    = string.Format("This is an {0}.  The check digit is ", iType);
                if (ISBN.Length == GetISBNTypeLen(iType))
                {
                    cd = CheckDigit;
                    if (!ISBN.EndsWith(cd))
                    {
                        Message += string.Format("incorrect: it should be {0}.", CheckDigit);
                        throw new ApplicationException(string.Format("Incorrect check digit: {0} entered, but should be {1}.", ISBN.Substring(ISBN.Length - 1, 1), cd));
                    }
                    else
                    {
                        Message += "correct.";
                    }
                }
                else
                {
                    Message += CheckDigit + ".";
                }
                Message += "\n";
                Message += string.Format("The full {0} is {1}.\n", iType, FullNum(ISBN));
                if (iType == ISBNTypeEnum.isEAN && ISBN.Substring(0, 3) == "977")
                {
                    Message += string.Format("It is for a serial publication with ISSN {0}.", FullNum(ISBN.Substring(3, 10)));
                }
                if (iType == ISBNTypeEnum.isEAN && ISBN.Substring(0, 4) == "9790")
                {
                    Message += string.Format("It is for a piece of music with ISMN {0}.", FullNum("M" + ISBN.Substring(4, 13)));
                }
                return(FullNum(ISBN));
            }
            else
            {
                if (ISBN.Length > 13 && ISBN.Substring(0, 3) == "977" && Valid(ISBN.Substring(0, 13), ISBNTypeEnum.isEAN))
                {
                    EAN = ISBN.Substring(0, 13);
                    if (EAN == FullNum(EAN))
                    {
                        Message  = string.Format("{0} is an EAN. The check digit is correct.\n", EAN);
                        Message += string.Format("It is for a serial publication with ISSN {0}, issue number {1}.", FullNum(ISBN.Substring(3, 10)), ISBN.Substring(13, ISBN.Length));
                        return(FullNum(ISBN.Substring(3, 10)));
                    }
                    else
                    {
                        Message = "You have typed in too much, too little, or letters instead of numbers.";
                        throw new ApplicationException(Message);
                    }
                }
                else
                {
                    Message = "You have typed in too much, too little, or letters instead of numbers.";
                    throw new ApplicationException(Message);
                }
            }
        }