Jump to content

# EAN8 Barcode generate Check Digit

## Recommended Posts

This is my contribution to generate the Check Digit of a barcode EAN8

```ConsoleWrite("--> _ean8_make_CheckDigit=" & _ean8_make_CheckDigit("0902221", 0) & @CRLF)
; ======================================================================================
; EAN8 Barcode generate Check Digit
Func _ean8_make_CheckDigit(\$digits, \$debug=0)
Local \$check_digit = ""
Local \$len = StringLen(\$digits)
If \$debug Then ConsoleWrite("\$digits=" & \$digits & @CRLF)
If \$debug Then ConsoleWrite("\$len=" & \$len & @CRLF)
If \$len == 7 Then
Local \$var0 = StringMid(\$digits, 1, 1)
Local \$var1 = StringMid(\$digits, 2, 1)
Local \$var2 = StringMid(\$digits, 3, 1)
Local \$var3 = StringMid(\$digits, 4, 1)
Local \$var4 = StringMid(\$digits, 5, 1)
Local \$var5 = StringMid(\$digits, 6, 1)
Local \$var6 = StringMid(\$digits, 7, 1)
; 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
Local \$even_sum = \$var1 + \$var3 + \$var5
; 2. Multiply this result by 3.
\$even_sum_three = \$even_sum * 3;
; 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
Local \$odd_sum = \$var0 + \$var2 +  \$var4 + \$var6
; 4. Sum the results of steps 2 and 3.
Local \$total_sum = \$even_sum_three + \$odd_sum;
; 5. The check character is the smallest number which, when added to the result in step 4,  produces a multiple of 10.
Local \$next_ten = (Ceiling(\$total_sum/10))*10;
\$check_digit = \$next_ten - \$total_sum;
If \$debug Then ConsoleWrite("\$check_digit=" & \$check_digit & @CRLF)
EndIf
return \$check_digit
EndFunc```

bye

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• Wiki

• Back

• Git