如何计算EAN-14的校验和数字?

时间:2016-12-08 15:42:14

标签: vba access-vba check-digit ean-13

我正在尝试在vba访问中验证EAN 14 UPC代码。我试图在网上找到它,但没有运气。我刚刚找到了EAN 8和EAN 13.所以,我只是尝试编写类似于EAN 13的代码如下:

If Len(Barcode) = 14 Then
    'do the check digit for EAN 14 for anything 14 long
    checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
                            + (Val(Mid(Barcode, 4, 1))) _
                            + (Val(Mid(Barcode, 6, 1))) _
                            + (Val(Mid(Barcode, 8, 1))) _
                            + (Val(Mid(Barcode, 10, 1))) _
                            + (Val(Mid(Barcode, 12, 1)))
        checkDigitSubtotal = (3 * checkDigitSubtotal) _
                            + (Val(Mid(Barcode, 1, 1))) _
                            + (Val(Mid(Barcode, 3, 1))) _
                            + (Val(Mid(Barcode, 5, 1))) _
                            + (Val(Mid(Barcode, 7, 1))) _
                            + (Val(Mid(Barcode, 9, 1))) _
                            + (Val(Mid(Barcode, 11, 1))) _
                            + (Val(Mid(Barcode, 13, 1)))

    If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
        Validate_UPC = "EAN14-BAD"
        Exit Function
    End If
        Validate_UPC = "EAN14-GOOD"
        Exit Function
    End If

它不起作用。我遇到的问题是虽然我输入了有效的EAN,它给了我EAN14-BAD。我认为我的验证代码不起作用。我刚刚添加了最后一行 + (Val(Mid(Barcode, 13, 1))) 在EAN13验证码上。请帮忙。

3 个答案:

答案 0 :(得分:0)

你试过8个字符吗?

If Len(Barcode) = 8 Then
    'do the check digit for EAN 8 for anything 8 long
    checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
                            + (Val(Mid(Barcode, 4, 1))) _
                            + (Val(Mid(Barcode, 6, 1))) 
        checkDigitSubtotal = (3 * checkDigitSubtotal) _
                            + (Val(Mid(Barcode, 1, 1))) _
                            + (Val(Mid(Barcode, 3, 1))) _
                            + (Val(Mid(Barcode, 5, 1))) _
                            + (Val(Mid(Barcode, 7, 1))) 

    If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
        Validate_UPC = "EAN8-BAD"
        Exit Function
    End If
    Validate_UPC = "EAN8-GOOD"
    Exit Function
End If

答案 1 :(得分:0)

当我将奇数乘以3时,它起作用如下:

 If Len(Barcode) = 14 Then       
    checkDigitSubtotal = (Val(Mid(Barcode, 1, 1))) _
                            + (Val(Mid(Barcode, 3, 1))) _
                            + (Val(Mid(Barcode, 5, 1))) _
                            + (Val(Mid(Barcode, 7, 1))) _
                            + (Val(Mid(Barcode, 9, 1))) _
                            + (Val(Mid(Barcode, 11, 1))) _
                            + (Val(Mid(Barcode, 13, 1)))
        checkDigitSubtotal = (3 * checkDigitSubtotal) _
                            + (Val(Mid(Barcode, 2, 1))) _
                            + (Val(Mid(Barcode, 4, 1))) _
                            + (Val(Mid(Barcode, 6, 1))) _
                            + (Val(Mid(Barcode, 8, 1))) _
                            + (Val(Mid(Barcode, 10, 1))) _
                            + (Val(Mid(Barcode, 12, 1)))

    If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
        Validate_UPC = "EAN14-BAD"
        Exit Function
    End If
        Validate_UPC = "EAN14-GOOD"
        Exit Function
    End If

答案 2 :(得分:0)

VB.NET中的EAN8和EAN13

  Public Function generateEAN(ByVal barcode As String, EsEan8 As Boolean) As String

    Dim first As Integer = 0
    Dim second As Integer = 0
    If EsEan8 Then
        barcode = Right(("00000000" & barcode), 7)
    Else
        barcode = Right(("000000000000" & barcode), 12)
    End If

    If barcode.Length() = 7 OrElse barcode.Length() = 12 Then

        For counter As Integer = 0 To barcode.Length() - 1 Step 2
            first = (first + barcode.Substring(counter, 1))

            If counter + 1 < barcode.Length Then
                second = (second + barcode.Substring(counter + 1, 1))
            End If

        Next
        If EsEan8 Then
            first = first * 3
        Else
            second = second * 3
        End If

        Dim total As Integer = second + first
        Dim roundedNum As Integer = (10 - (total Mod 10)) Mod 10
        barcode = barcode & roundedNum

    End If
    Return barcode



End Function