Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim CodeClair As String = "001034360548"
CodeClair = CodeClair.PadRight(12, "0"c)
MsgBox(EAN13(CodeClair))
End Sub
Public Function EAN13(ByVal chaine As String) As String
Dim i As Integer
Dim first As Integer
Dim checksum As Integer = 0
Dim CodeBarre As String = ""
Dim tableA As Boolean
If Regex.IsMatch(chaine, "^\d{12}$") Then
For i = 1 To 11 Step 2
System.Diagnostics.Debug.WriteLine(chaine.Substring(i, 1))
checksum += Convert.ToInt32(chaine.Substring(i, 1))
Next
checksum *= 3
For i = 0 To 11 Step 2
checksum += Convert.ToInt32(chaine.Substring(i, 1))
Next
chaine += (10 - checksum Mod 10) Mod 10
CodeBarre = chaine.Substring(0, 1) & ChrW(65 + Convert.ToInt32(chaine.Substring(1, 1)))
first = Convert.ToInt32(chaine.Substring(0, 1))
For i = 2 To 6
tableA = False
Select Case i
Case 2
If first >= 0 AndAlso first <= 3 Then
tableA = True
End If
Exit Select
Case 3
If first = 0 OrElse first = 4 OrElse first = 7 OrElse first = 8 Then
tableA = True
End If
Exit Select
Case 4
If first = 0 OrElse first = 1 OrElse first = 4 OrElse first = 5 OrElse first = 9 Then
tableA = True
End If
Exit Select
Case 5
If first = 0 OrElse first = 2 OrElse first = 5 OrElse first = 6 OrElse first = 7 Then
tableA = True
End If
Exit Select
Case 6
If first = 0 OrElse first = 3 OrElse first = 6 OrElse first = 8 OrElse first = 9 Then
tableA = True
End If
Exit Select
End Select
If tableA Then
CodeBarre += ChrW(65 + Convert.ToInt32(chaine.Substring(i, 1)))
Else
CodeBarre += ChrW(75 + Convert.ToInt32(chaine.Substring(i, 1)))
End If
Next
CodeBarre += "*"
'Ajout s้parateur central / Add middle separator
For i = 7 To 12
CodeBarre += ChrW(97 + Convert.ToInt32(chaine.Substring(i, 1))) <<<<< Error บันทัดนี้นะครับ
Next
CodeBarre += "+"
End If
Return CodeBarre
End Function
Error :
Index and length must refer to a location within the string.
Parameter name: length
Public Function EAN13(ByVal chaine As String) As String
Dim i As Integer
Dim first As Integer
Dim checksum As Integer = 0
Dim CodeBarre As String = ""
Dim tableA As Boolean
If Microsoft.VisualBasic.Len(chaine) = 12 Then
If Convert.ToInt64(chaine) >= 100000000000 Then
For i = 1 To 11 Step 2
System.Diagnostics.Debug.WriteLine(chaine.Substring(i, 1))
checksum += Convert.ToInt32(chaine.Substring(i, 1))
Next
checksum *= 3
For i = 0 To 11 Step 2
checksum += Convert.ToInt32(chaine.Substring(i, 1))
Next
chaine = chaine & (10 - checksum Mod 10) Mod 10
CodeBarre = chaine.Substring(0, 1) & ChrW(65 + Convert.ToInt32(chaine.Substring(1, 1)))
first = Convert.ToInt32(chaine.Substring(0, 1))
For i = 2 To 6
tableA = False
Select Case i
Case 2
If first >= 0 AndAlso first <= 3 Then
tableA = True
End If
Exit Select
Case 3
If first = 0 OrElse first = 4 OrElse first = 7 OrElse first = 8 Then
tableA = True
End If
Exit Select
Case 4
If first = 0 OrElse first = 1 OrElse first = 4 OrElse first = 5 OrElse first = 9 Then
tableA = True
End If
Exit Select
Case 5
If first = 0 OrElse first = 2 OrElse first = 5 OrElse first = 6 OrElse first = 7 Then
tableA = True
End If
Exit Select
Case 6
If first = 0 OrElse first = 3 OrElse first = 6 OrElse first = 8 OrElse first = 9 Then
tableA = True
End If
Exit Select
End Select
MsgBox(Convert.ToInt32(chaine.Substring(i, 1)))
If tableA Then
CodeBarre += ChrW(65 + Convert.ToInt32(chaine.Substring(i, 1)))
Else
CodeBarre += ChrW(75 + Convert.ToInt32(chaine.Substring(i, 1)))
End If
Next
CodeBarre += "*"
For i = 7 To 12
CodeBarre += ChrW(97 + Convert.ToInt32(chaine.Substring(i, 1)))
Next
CodeBarre += "+"
End If
End If
Return CodeBarre
End Function