Skip to content

Instantly share code, notes, and snippets.

@hexbinoct
Created November 1, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hexbinoct/a5f3ec754c5d439473fb2030f4e3682a to your computer and use it in GitHub Desktop.
Save hexbinoct/a5f3ec754c5d439473fb2030f4e3682a to your computer and use it in GitHub Desktop.
Visual Basic .net code to convert input to Code128 barcode font.
'taken from https://grandzebu.net/informatique/codbar-en/code128.htm please visit their website for more information.
'code below is working fine in vb.net, original code was older version, before .net days I think.
'imp link
'https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/type-characters
'type characters while declaring variables
'symbol Type Example
'% Integer Dim L%
'& Long Dim M&
'@ Decimal Const W@ = 37.5
'! Single Dim Q!
'# Double Dim X#
'$ String Dim V$ = "Secret"
'------------------------------------------------------------------------------
Public Class barcode
Public Function barprocess(str$)
barprocess = my128(str$)
End Function
Public Function my128$(myinput$)
'V 2.0.0
'Parametres : une chaine
'Parameters : a string
'Retour : * une chaine qui, affichee avec la police my128.TTF, donne le code barre
' * une chaine vide si parametre fourni incorrect
'Return : * a string which give the bar code when it is dispayed with my128.TTF font
' * an empty string if the supplied parameter is no good
Dim i%, checksum&, mini%, dummy%, tableB
my128$ = ""
If Len(myinput$) > 0 Then
'Verifier si caracteres valides
'Check for valid characters
For i% = 1 To Len(myinput$)
Select Case Asc(Mid$(myinput$, i%, 1))
Case 32 To 126, 203
Case Else
i% = 0
Exit For
End Select
Next
'Calculer la chaine de code en optimisant l'usage des tables B et C
'Calculation of the code string with optimized use of tables B and C
my128$ = ""
tableB = True
If i% > 0 Then
i% = 1 'i% devient l'index sur la chaine / i% become the string index
Do While i% <= Len(myinput$)
If tableB Then
'Voir si interessant de passer en table C / See if interesting to switch to table C
'Oui pour 4 chiffres au debut ou a la fin, sinon pour 6 chiffres / yes for 4 digits at start or end, else if 6 digits
mini% = IIf(i% = 1 Or i% + 3 = Len(myinput$), 4, 6)
'GoSub testnum
dash(mini, myinput, i)
If mini% < 0 Then 'Choix table C / Choice of table C
If i% = 1 Then 'Debuter sur table C / Starting with table C
my128$ = Chr(210)
Else 'Commuter sur table C / Switch to table C
my128$ = my128$ & Chr(204)
End If
tableB = False
Else
If i% = 1 Then my128$ = Chr(209) 'Debuter sur table B / Starting with table B
End If
End If
If Not tableB Then
'On est sur la table C, essayer de traiter 2 chiffres / We are on table C, try to process 2 digits
mini% = 2
'GoSub testnum
dash(mini, myinput, i)
If mini% < 0 Then 'OK pour 2 chiffres, les traiter / OK for 2 digits, process it
dummy% = Val(Mid$(myinput$, i%, 2))
dummy% = IIf(dummy% < 95, dummy% + 32, dummy% + 105)
my128$ = my128$ & Chr(dummy%)
i% = i% + 2
Else 'On n'a pas 2 chiffres, repasser en table B / We haven't 2 digits, switch to table B
my128$ = my128$ & Chr(205)
tableB = True
End If
End If
If tableB Then
'Traiter 1 caractere en table B / Process 1 digit with table B
my128$ = my128$ & Mid$(myinput$, i%, 1)
i% = i% + 1
End If
Loop
'Calcul de la cle de controle / Calculation of the checksum
For i% = 1 To Len(my128$)
dummy% = Asc(Mid$(my128$, i%, 1))
dummy% = IIf(dummy% < 127, dummy% - 32, dummy% - 105)
If i% = 1 Then checksum& = dummy%
checksum& = (checksum& + (i% - 1) * dummy%) Mod 103
Next
'Calcul du code ASCII de la cle / Calculation of the checksum ASCII code
checksum& = IIf(checksum& < 95, checksum& + 32, checksum& + 105)
'Ajout de la cle et du STOP / Add the checksum and the STOP
my128$ = my128$ & Chr(checksum&) & Chr(211)
End If
End If
Exit Function
'testnum:
' 'si les mini% caracteres a partir de i% sont numeriques, alors mini%=0
' 'if the mini% characters from i% are numeric, then mini%=0
' mini% = mini% - 1
' If i% + mini% <= Len(chaine$) Then
' Do While mini% >= 0
' If Asc(Mid$(chaine$, i% + mini%, 1)) < 48 Or Asc(Mid$(chaine$, i% + mini%, 1)) > 57 Then Exit Do
' mini% = mini% - 1
' Loop
' End If
' Return
End Function
Sub dash(ByRef mini%, ByRef chaine$, ByRef i%)
mini% = mini% - 1
If i% + mini% <= Len(chaine$) Then
Do While mini% >= 0
If Asc(Mid$(chaine$, i% + mini%, 1)) < 48 Or Asc(Mid$(chaine$, i% + mini%, 1)) > 57 Then Exit Do
mini% = mini% - 1
Loop
End If
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment