Exemplo n.º 1
0
        public static string ConvertISBNToEAN(this string TargetISBN, string EANPrefix = "978")
        {
            int    nCheckSum;
            int    nTemp;
            string EAN;
            string ISBN;

            // Initialization
            nTemp     = 0;
            nCheckSum = 0;
            EAN       = "";

            if (TargetISBN.Length < CONST_ISBN_LEN)
            {
                ISBN = TargetISBN.PadLeft(CONST_ISBN_LEN, '0');
            }
            else
            {
                ISBN = TargetISBN;
            }

            if (!ISBN.IsISBNValid())
            {
                return("");
            }

            if (EANPrefix.Length != 3)
            {
                return("");
            }

            EAN = EANPrefix + ISBN.Substring(0, (CONST_ISBN_LEN - 1));

            nCheckSum = 0;
            for (uint i = 0; i < EAN.Length; i++)
            {
                char cTempVal = EAN.ToCharArray()[i];

                // Do not convert the number directly from a char, since we do not want the ASCII value
                // nTemp = Convert.ToInt32(cTempVal);

                nTemp = Convert.ToInt32(new String(new char[] { cTempVal }));

                if (nTemp > 0)
                {
                    if ((i % 2) == 0)
                    {
                        nCheckSum += nTemp;
                    }
                    else
                    {
                        nCheckSum += (3 * nTemp);
                    }
                }
            }

            nCheckSum = (CONST_ISBN_LEN - (nCheckSum % 10)) % 10;

            EAN += Convert.ToString(nCheckSum);

            return(EAN);
        }