我如何验证UPC或EAN code? [英] How do I validate a UPC or EAN code?

查看:508
本文介绍了我如何验证UPC或EAN code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个C#.NET函数来计算一个类型或扫描吧code是否是一个有效的全球贸易项目代码(UPC或EAN)。

的吧code编号的最后一位是计算机校验位,这使得确保了吧code组成正确。 GTIN校验位计算器

解决方案

 公共静态布尔IsValidGtin(字符串code)
{
    如果(code!=(新的正则表达式([^ 0-9]))。更换(code,))
    {
        //不是数字
        返回false;
    }
    //垫零延长至14位
    开关(code.Length)
    {
        案例8:
            code =000000+ code;
            打破;
        案例12:
            code =00+ code;
            打破;
        案例13:
            code =0+ code;
            打破;
        案例14:
            打破;
        默认:
            //数字错号
            返回false;
    }
    //计算校验位
    INT [] A =新的INT [13];
    A [0] = int.Parse(code [0]的ToString())* 3;
    A [1] = int.Parse(code [1]的ToString());
    一个[2] = int.Parse(code [2]的ToString())* 3;
    一[3] = int.Parse(code [3]的ToString());
    一个[4] = int.Parse(code [4]的ToString())* 3;
    一个[5] = int.Parse(code [5]的ToString());
    一个[6] = int.Parse(code [6]的ToString())* 3;
    一个[7] = int.Parse(code [7]的ToString());
    一[8] = int.Parse(code [8]的ToString())* 3;
    一个[9] = int.Parse(code [9]的ToString());
    一[10] = int.Parse(code [10]的ToString())* 3;
    一[11] = int.Parse(code [11]的ToString());
    一[12] = int.Parse(code [12]的ToString())* 3;
    INT总和=一个[0] +一[1] + A [2] + A [3] +一[4] + A [5] +一[6] + A [7] +一[8] +一〔 9] +一[10] +一[11] +一[12];
    INT检查=(10  - (合计10%))%10;
    //计算校验位
    INT最后= int.Parse(code [13]的ToString());
    回查==最后;
}
 

I need a C# .NET function to evaluate whether a typed or scanned barcode is a valid Global Trade Item Number (UPC or EAN).

The last digit of a bar code number is a computer Check Digit which makes sure the bar code is correctly composed. GTIN Check Digit Calculator

解决方案

public static bool IsValidGtin(string code)
{
    if (code != (new Regex("[^0-9]")).Replace(code, ""))
    {
        // is not numeric
        return false;
    }
    // pad with zeros to lengthen to 14 digits
    switch (code.Length)
    {
        case 8:
            code = "000000" + code;
            break;
        case 12:
            code = "00" + code;
            break;
        case 13:
            code = "0" + code;
            break;
        case 14:
            break;
        default:
            // wrong number of digits
            return false;
    }
    // calculate check digit
    int[] a = new int[13];
    a[0] = int.Parse(code[0].ToString()) * 3;
    a[1] = int.Parse(code[1].ToString());
    a[2] = int.Parse(code[2].ToString()) * 3;
    a[3] = int.Parse(code[3].ToString());
    a[4] = int.Parse(code[4].ToString()) * 3;
    a[5] = int.Parse(code[5].ToString());
    a[6] = int.Parse(code[6].ToString()) * 3;
    a[7] = int.Parse(code[7].ToString());
    a[8] = int.Parse(code[8].ToString()) * 3;
    a[9] = int.Parse(code[9].ToString());
    a[10] = int.Parse(code[10].ToString()) * 3;
    a[11] = int.Parse(code[11].ToString());
    a[12] = int.Parse(code[12].ToString()) * 3;
    int sum = a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + a[10] + a[11] + a[12];
    int check = (10 - (sum % 10)) % 10;
    // evaluate check digit
    int last = int.Parse(code[13].ToString());
    return check == last;
}

这篇关于我如何验证UPC或EAN code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆