Convert number to barcode Code 128

I just dropped .++"")q into word printed it and scanned it using a barcode scanner I have here, it came out with 95141111020209 :slight_smile:

Here you go Steve, this is some REALLY nasty code converted from VB on that site, it works though.

Just call it with:

dim a as string
a = Code128(“95141111020209”)

[code]Public Function Code128(CHAINEEE As String) as String
'V 2.0.0
'Parametres : une chaine
'Parameters : a string
'Retour : * une chaine qui, affichee avec la police CODE128.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 CODE128.TTF font
’ * an empty string if the supplied parameter is no good
Dim i As Integer, checksum As Integer, mini As Integer, dummy As Integer, tableB As Boolean
Dim CODE128RETURN As String
CODE128RETURN = “”
If Len(CHAINEEE) > 0 Then
'Verifier si caracteres valides
'Check for valid characters
For i = 1 To Len(CHAINEEE)
Select Case Asc(Mid(CHAINEEE, 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
CODE128RETURN = “”
tableB = True
If i > 0 Then
i = 1 'i% devient l’index sur la chaine / i% become the string index
While i <= Len(CHAINEEE)
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 = If (i = 1 Or i + 3 = Len(CHAINEEE), 4, 6)
Goto testnum1
back1:
If mini < 0 Then 'Choix table C / Choice of table C
If i = 1 Then 'Debuter sur table C / Starting with table C
CODE128RETURN = Chr(210)
Else 'Commuter sur table C / Switch to table C
CODE128RETURN = CODE128RETURN + Chr(204)
End If
tableB = False
Else
If i = 1 Then CODE128RETURN = 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
Goto testnum2
back2:
If mini < 0 Then 'OK pour 2 chiffres, les traiter / OK for 2 digits, process it
dummy = Val(Mid(CHAINEEE, i, 2))
dummy = If (dummy < 95, dummy + 32, dummy + 105)
CODE128RETURN = CODE128RETURN + 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
CODE128RETURN = CODE128RETURN + Chr(205)
tableB = True
End If
End If
If tableB Then
'Traiter 1 caractere en table B / Process 1 digit with table B
CODE128RETURN = CODE128RETURN + Mid(CHAINEEE, i, 1)
i = i + 1
End If
Wend
'Calcul de la cle de controle / Calculation of the checksum
For i = 1 To Len(CODE128RETURN)
dummy = Asc(Mid(CODE128RETURN, i, 1))
dummy = If (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 = If (checksum < 95, checksum + 32, checksum + 105)
'Ajout de la cle et du STOP / Add the checksum and the STOP
CODE128RETURN = CODE128RETURN + Chr(checksum) + Chr(211)
End If
End If
Return CODE128RETURN

testnum1:
'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(CHAINEEE) Then
While mini >= 0
If Asc(Mid(CHAINEEE, i + mini, 1)) < 48 Or Asc(Mid(CHAINEEE, i + mini, 1)) > 57 Then Exit While
mini = mini - 1
Wend
End If
Goto back1

testnum2:
'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(CHAINEEE) Then
While mini >= 0
If Asc(Mid(CHAINEEE, i + mini, 1)) < 48 Or Asc(Mid(CHAINEEE, i + mini, 1)) > 57 Then Exit While
mini = mini - 1
Wend
End If
Goto back2
End Function
[/code]

Just FYI, if you end up with the upper image (like the start of a new barcode after it) make sure you put the caret at the end of the barcode then change the font off of code128 (any font will do):

Well thank you so much Julian, that code was EXACTLY what I was after. :slight_smile: :slight_smile: :slight_smile: I was always confident it could be simplified by using a font - but I didn’t know how to.

It did take me a while to work out how to create a method that returns a value (function) it’s been a while and I’ve never really got my head around it. Nevertheless, it works perfectly and the rest of the implementation, I shouldn’t have a problem with.

I’m not sure what you mean above, I haven’t seen that issue with testing this or other code numbers yet.?

Anyway Julian, your efforts, mine, and others who have helped may in the end come to nothing and never see the light of day.

There is a lot of political bs going on at my work at the moment, if I told you the whole story it may well rival the best Shakespearean play!

I will however do my best to use the code provided to create a simple app that reads the data, encodes it, then saves as a cvs file with 1 column showing the “human readable” text, and the other the encoded text which can then be imported into Indesign with the appropriate fonts applied.

The last time this client job was done, the barcodes were created using a commercial barcode program which was done by someone else. The whole procedure of inserting 80,000 pdf images then imposing up to 20 to view on an SRA3 sheet took approx. 2 working days (16hrs).

Given the code kindly converted By Julian and written by the original author, then I estimate the whole procedure would take approx. 4hrs. Unfortunately the company that I work for never seems to appreciate these time savings ideas that I like to implement. :frowning:

Come Monday morning, I’ll see we where the cards lie.

[EDIT] As I mentioned previously, my day job is as a pre-press professional in a printing company, not as a software developer. My interest in programming is only for fun or to challenge my brain/mind thingy. It’s not part of my job description.

Well the cards have fallen in a random mess on the ground. I’m going ahead with the final code but I’ve come up with an error.

I’ve got this working with basic file I/O for testing, but I cant remember how to loop through the whole file.

I get the error: Type mismatch error. Expected boolean, but got integer. There is something I have forgotten. This is the code:

[code]For i As Integer = 1 to numbersFile.EOF

readData = numbersFile.ReadLine
finalEncodedData = EncodeCode128(readData)
writeEncodedData.Writeline (readData + Delimiter + finalEncodedData)

Next i[/code]

What I’m wanting to do is loop through the file until it ends, reading every line of data. A syntax error I guess, but not obvious to me at the moment. I’ll pursue but hopefully someone can answer and save me some time. A single run through works as expected, the for/next loop doesn’t.

dim i as integer = 1
while not numbersFile.EOF
...
i=i+1
wend

Thank you Jean-Yves that has worked perfectly! :slight_smile:

I’ve never ever used while/wend. My logic always goes with the for/next loop. This proves the positive case for while/wend.

Almost 11pm here and with everyone’s help I’ve created the final file. 80,000 lines of a csv file. ie. The original serial number plus the encoded number. Tomorrow morning I have to “present” this as a solution. If it fails it’s no big deal but it’s looking very good.

One thing I’m not sure of is the character I’ve used as the delimiter. Ive use the standard comma. But now I’m wondering what would happen if the comma was part of the “encoded” text. The encoded text includes non-standard ascii characters, so if one of those was the comma, then what would happen?

If a csv reads from left to right to the delimeter, then maybe it’s ok. OR I could us a tab as the delimeter?

Use chr(9) for tab delimited.

That works and is a good option, thanks Greg. I’ll probably leave it using the tab instead of the comma - safer I think.

This is now finished enough to be used in the real world. No open/save dialog boxes, but hard-coded filenames and directories. Not much point going further.

I often use a “;” as a csv delimiter. it’s less frequent in texts than a comma. but it happens …
then I use chr(1) as a separator, and chr(2) as a record separator
but then you have to adapt the import method too .

Ok, so now I’ve emailed all the files to my work. It will be an interesting day tomorrow. Like I originally said, this may become a rod for my own back, but perhaps I like that sort of thing :slight_smile:

ACT ONE:
Today I had a discussion with my boss about how we can create the barcodes. Previously it was done by a work colleague that doesn’t like to share knowledge and also used a barcode programme that he got from his previous employer. Likely Illegal software, and regardless if it was or not, it was never purchased by my company.

That “other” person’s computer f*kd up, and his attitude was “why should I do this”?. I agree, he should never have held the company to ransom (political bs).

ACT TWO:
Steve Kelepouris comes into the scene (stage left) “You know what boss, I reckon there is a better solution”, instead of spending money ($600AUD) on a barcode program I think I can come up with a solution that will work and save the company money.

Boss: I’m prepared to spend $600 to get this done and sorted.

Steve: I don’t think we need to do that, I’m working on a solution, it’s looking good. Not only will it save you money, but also save you time (money as well)

Boss: Well, I don’t care much about what it costs because it will be covered by the client over time.

Steve: That’s good, so in order for this/my solution to work, as a backup it would be nice to have a barcode reader to check things, you no, since we do this sort of thing often enough - it only costs around $100 and it’s a useful tool, afterall, we are professionals.

Boss: Well can’t we just use an iPhone app etc… to read the barcode?


I think I accidentally posted before I was finished (possibly a good thing) The upshot is that my boss is prepare to outlay more money than necessary and can’t see the value in a physical barcode reader. Utter madness.

See a need, fill a need.

Ahh bosses :slight_smile:

I would love Functions in Functions just to avoid to see GoTos and Labels.

Public Function F1(n As Integer) As Integer

   Function Add21or31(n2 as Integer) As Integer  // They are always local, no public, private...
       If n2 mod 2 = 0 Then Return n2+21 // Even, Add 21
       Return n2+31 // Odd, Add 31 
   End

   Function Add44(n3 as Integer) As Integer  // They are always local, no public, private...
           Return n3+44
   End

  // Return a Call add44 after calling Function Add21or31 twice because whatever
  Return Add44(Add21or31(Add21or31(n))) 

End

Well, it’s been well over a week since this solution was implemented. Out of courtesy to those that have helped, then I am responding, and thanks all.

As far as I can see it has worked perfectly.

The longevity is what I don’t know, but I will stick my neck out to say - solved.

The voucher codes were set to the middle of March, so I/we/they won’t really know until that time has passed. I’m confident that it will.

I’m the “Citizen Programmer” where I work, and the fact that I can develop a programing solution is kudos to Xojo. The help I have got is great, but programming solutions is not part of my job description. I’ve spent many many hours on this and other solutions that has saved my/the other company many many hours of work and many many thousands of dollars. - Sound familiar?

When/if the next job comes up, then I’ll push the point, but mostly people can’t see that point. The point will be laying my job on the line - it has to be done. I’m pretty much done with the stupidity.

I absolutely appreciate all the input. Let’s see how the person with some brains and tools and the ideas succeed. I suspect not very well - unless you are an A’hole.

That probably doesn’t make sense. I’m not in a good place at the moment. I’ll report back soon as I can.

Julians code is working pretty good on Labels with code128 as webstyle, but how can i get the barcode in a htmlviewer or picture? all my tries with g.drawstring or css in htmlviewer are failed. if i create an html file on my desktop and open it in browser everthing is working with the code128 font. Have anyone a solution? excuse my english. Best regards Stefan

not clear to me if you’re creating a web app and want to show the code128 or a desktop app
which are you working on ?
in a desktop app you shouldnt need an html viewer

[quote=451228:@Norman Palardy]not clear to me if you’re creating a web app and want to show the code128 or a desktop app
which are you working on ?
in a desktop app you shouldnt need an html viewer[/quote]

Hello Norman, im working on a webapp. If the font code128.ttf is installed on the client machine it shows on webapp my prefered barcode on the label (wich has an own webstyle with the text-family: code128 font). The htmlviewer shows only the converted raw text as plain inside. if i export the same html code which was loaded into htmlviewer control on my desktop as *.html file via folderitem and run it on google chrome the code will displayed as barcode correctly. i also converted the code128.ttf to an *.woff webfont and put it on the directory of the webapp but the problem hasnt cleared. outside of the htmlviewer control everthing works correctly. I put the html code in the pagesource for testing and my Barcode appears in the background of my webapp. I think the htmlviewer control renders not correctly.

THX for your answer!

The reason it will show on your local machine is because you have the code128 font installed so its accessible to the web browser for use
In the web app, which would normally run on a server, the font would need to be installed there. Then if you draw the result into a picture and then embed that picture in a web page you should see the bar code in what ever web browser is used since its just an image.

The desktop app with an HTMLViewer will vary depending on which desktop OS your running (macOS, Windows, Linux)
On Windows you could be using either the native web browser control, which can vary widely in what version of IE/Edge its based on, or WebKit. And this can lead to very different behaviour.

I’d focus on writing the web app and getting that to display the barcode in Chrome or whatever browser you normally use and not worry about creating the desktop app to test things UNLESS the desktop app is crucial to the software you’re writing

[quote=451320:@Norman Palardy]The reason it will show on your local machine is because you have the code128 font installed so its accessible to the web browser for use
In the web app, which would normally run on a server, the font would need to be installed there. Then if you draw the result into a picture and then embed that picture in a web page you should see the bar code in what ever web browser is used since its just an image.

The desktop app with an HTMLViewer will vary depending on which desktop OS your running (macOS, Windows, Linux)
On Windows you could be using either the native web browser control, which can vary widely in what version of IE/Edge its based on, or WebKit. And this can lead to very different behaviour.

I’d focus on writing the web app and getting that to display the barcode in Chrome or whatever browser you normally use and not worry about creating the desktop app to test things UNLESS the desktop app is crucial to the software you’re writing[/quote]

Hi, Norman on my local machine the htmlviewer also dont render the barcode as barcode. my local machine emulates momentan the server. every client has the font installed, the htmlviewer doesnt work. the graphic.drawstring also dont work for me