Convert number to barcode Code 128

  1. last year

    Steve K

    24 Jan 2018 Melbourne, Australia
    Edited last year

    In the past I've created barcodes by using code number supplied and a font (free 3 of 9).

    This time I need to create the barcodes in Code 128 symbology. As far as I can understand the text has to be converted using a calculation/formula.

    I have the font Code 128 and have used an on-line generator to encode some sample text, eg. 95141111020209 which ends up: ÍÃ.++"")qÎ

    When I apply the font it doesn't match the known working samples I have.

    Any help of which way forward appreciated.

    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")

    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
  2. Tim P

    24 Jan 2018 Pre-Release Testers Rochester, NY

    If plugins are an option, MBS has a barcode plugin.

  3. Steve K

    24 Jan 2018 Melbourne, Australia
    Edited last year

    Thanks Tim, the MBS plugin is probably not what I need. I think I did look at that and it creates a pdf of the barcode which is not really what I'm after.

    This is a work related project and something that I don't have the time to code in Xojo myself because the encoding of 128 is too complicated.

    The barcodes will be used in an InDesign document. These are for vouchers which there are approx. 80,000 of them!!

    Normally this type of thing isn't that hard because we have other plugins that work with (free of 9) barcodes which are in a database that and are simply created by applying the correct font (similar to datamerge).

    All I really need is to be able to convert the number, which then "encodes" to the correct format - then I believe I can apply the correct font.

    There are commercial plugins but these are very expensive. There is an old excel macro, but I don't trust it.

    Perhaps someone has already a simple tool that can do the conversion.? We could come to some arrangement for payment. Like I said, I don't really want to create my own software from scratch.

  4. Douglas H

    24 Jan 2018 Pre-Release Testers, Xojo Pro

    @SteveKelepouris Thanks Tim, the MBS plugin is probably not what I need. I think I did look at that and it creates a pdf of the barcode which is not really what I'm after.

    Actually, no, it creates a picture object with the graphics for the desired barcode.

    Barcode generators tend to run it two main groups: those that generate graphics (such as MBS) and those that are designed for use with a specific barcode "font". The thing about these fonts is they tend to require text conversion algorithms which are specific to the vendor/designer of the "font" and barcode type.

    @SteveKelepouris Perhaps someone has already a simple tool that can do the conversion.? We could come to some arrangement for payment. Like I said, I don't really want to create my own software from scratch.

    For the barcode "fonts" I have used in the past, the conversion algorithm had to be supplied by the font vendor because only they know which code points represent given bar patterns. And the conversion was different for different barcodes, because the bar patterns are different. (Say Code 3 of 9 vs Code 128) I've also used different font vendors for a given font and they needed different conversion algorithms.

    You either need a font AND conversion formula from the same vendor, OR you need to use a graphics barcode like MBS instead of a font based barcode.

  5. Steve K

    24 Jan 2018 Melbourne, Australia

    Thanks Douglas, I didn't think it would be that simple.

    The MBS plugin is for Xojo, ie. it works "within" Xojo, correct? Therefore I would still have to come up with a way to implement it.

    I do have details for the conversion formula, and also have a font - not sure if they would match though and it would take me a lot of effort to do the programming. I could also create my own font.

    Like is said, this is a lot of work. My day job is in prepress for a printing company not a software developer so even if I did come up with something in my "own" time, I would likely be making a rod for my own back.

    Thanks for the help anyway.

  6. Douglas H

    24 Jan 2018 Pre-Release Testers, Xojo Pro

    @SteveKelepouris The MBS plugin is for Xojo, ie. it works "within" Xojo, correct? Therefore I would still have to come up with a way to implement it.

    Correct. If the other output is not created by Xojo, then using a font based barcode is likely the easier solution.

    @SteveKelepouris I do have details for the conversion formula, and also have a font - not sure if they would match though and it would take me a lot of effort to do the programming. I could also create my own font.

    The conversion formula and font need to be from the same author. I also don't think you should try to create your own font.

    I'd suggest you find the vendor/author of the font you have and obtain their conversion formula. They may not have a Xojo example, but they are likely to have a VB sample that would not be hard to migrate.

    Or else find another font with a conversion formula that goes with it. A quick web search includes this link to a free Code 128 font. The page also has a link to a page it says contains code samples in various programming languages. It also says that page is in French. I did not pursue it though.

  7. Steve K

    24 Jan 2018 Melbourne, Australia

    Yeah, I've seen and read that link before. It may well be the source of the font I have.
    I did muck around with an online encoder just to see if the font worked but there were some random erroneous spaces added.

    The font is Code128.ttf and I believe it's the only version one floating around on the web.

    I may investigate a bit further before I completely can the whole idea. The reality is that even if I came up with a solution, I would get nothing for it, except for a bit of kudos maybe - baaahhh! kudos is overrated.

  8. Jean-Yves P

    24 Jan 2018 Pre-Release Testers, Xojo Pro Europe (France, Besançon)
    Edited last year

    here is a link to a free barcode generator source code.
    I downloaded an old 1.0b version that did only powerpc and updated it to v2 which works with current systems.
    no warranty, use at your own risk...
    https://drive.google.com/open?id=1-LgWsmrHXsnpxZRhDXIJGgAe2KvF6ymO

    they generate code128.
    it's an xcode project.

    here is the compiled app :
    https://drive.google.com/open?id=15HqL98wQ33sPDDinMoWus72jjKAIMHSm

  9. Tim H

    25 Jan 2018 Pre-Release Testers Portland, OR USA

    I pulled this out of an old project. I think this is the code128 piece, but I don't know the font used. Does it look remotely similar to the algorithm you have? It might help you find out where your code is failing.

    dim bar as string    // the value to encode
    dim barcod as string  // the result to apply the font to
    dim n as integer
    
    if bar.len mod 2<> 0 then bar= "0"+ bar
    for i= 1 to bar.len step 2
       n= val(mid(bar, i, 2))
       if n<= 93 then
          n= n+ 33
       else
          n= n+ 103
       end
       barcod= barcod+ chr(n)
    next
    barcod= chr(203)+ barcod+ chr(204)

    This code was "left over" as a commented out section after we switched to 3 of 9, so it may not even be code128, although my failing memory thinks it is.

  10. Julian S

    25 Jan 2018 Pre-Release Testers, Xojo Pro UK
    Edited last year

    EDIT

    BLEH, just noticed my code is for EAN 13 and you need Code 128 :(

    EDIT

    How does

    ÒÈ.++"")qÓ

    look for

    95141111020209

    ?

  11. Steve K

    25 Jan 2018 Melbourne, Australia

    Thanks guys, your help is very much appreciated.

    @ Jean-Yves: Thanks for your efforts. I'll have to wait till I get back to my work on Monday to check it out as I only have a windows machine at home. If it creates a graphic object then I would prefer not to go down that path.

    @ Tim: I'm not sure what that is. There are some similarities to 128.
    I haven't constructed the algorithm or any code yet. This is what I have looked at:
    http://www.barcodeisland.com/code128.phtml which is a good explanation of the symbology.

    Computing the "checksum" digit is probably the most complicated part. I know what modulo is but I've read it a few times and it still hasn't sunk in yet.

    Regardless, the font needs to suit also. There is no reason why the client could not just use 3 of 9 as it's much simpler - but Code 128 has the advantage of higher density within a smaller space - although not critical in this instance because they have plenty of space on the artwork to insert it.

    All I need is a simple 2 column table. Column 1 shows the "English Readable" code and column 2 show the gobblygook text that converts to the barcode when the font is applied. I explored a formula in excel that someone had created but it didn't look right.

    Come to think of it, I wonder if there is a difference in encoding of the characters, ie. mac vs windows and that's the reason it didn't work?. I've only tried this on a mac.

  12. Emile S

    25 Jan 2018 Europe (France, Strasbourg)

    @SteveKelepouris modulo

    this is that in Xojo: http://developer.xojo.com/mod

  13. Emile S

    25 Jan 2018 Europe (France, Strasbourg)
    Edited last year

    @SteveKelepouris I've only tried this on a mac.

    If you do not have a Windows Hardware handly, you can install VirtualBox from:

    https://www.virtualbox.org/wiki/Downloads

  14. Steve K

    25 Jan 2018 Melbourne, Australia
    Edited last year

    Thanks Emile.

    Sorry, my wording wasn't clear.

    re. "I know what modulo is but I've read it a few times and it still hasn't sunk in yet."

    I meant to say:

    "I understand what modulo is, and I've read the explanation on Computing the "checksum" digit a few times, and it still hasn't sunk in yet exactly how the checksum is calculated".

    At my work I have both Windows and MacOS, but at home only Windows.

  15. Emile S

    25 Jan 2018 Europe (France, Strasbourg)

    OK.

    @SteveKelepouris At my work I have both Windows and MacOS, but at home only Windows.

    Download VirtualBox and install macOS on your Windows Hardware ;)
    (I do not try that).

  16. Steve K

    25 Jan 2018 Melbourne, Australia
    Edited last year

    Oh! I didn't know you could do that - thanks for the tip.

    But, that's not really going to help because I don't have a Mac version of InDesign, or any other Mac software to test things. I've got an old version of InDesign on my home windows machine that I can test with.

    Like I said, all I need is a 2 column table or csv file. It's not about the machine but about how to encode the data correctly.

  17. Christian S

    25 Jan 2018 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    Please download MBS plugins, put the barcode and DynaPDF plugins in your plugins folder and play with examples.

    Our plugin can do barcode 128 as picture and as vector graphic and both can go in a PDF.

  18. Paul S

    25 Jan 2018 Pre-Release Testers, Xojo Pro Europe (Netherlands, Den Haag)

    I don’t know if you are familiar with Einhugur plugins.
    Einhugur (Bjorn Eiriksson) also has a very nice BarcodePlugin

  19. Steve K

    25 Jan 2018 Melbourne, Australia
    Edited last year

    Thanks Christian & Paul, much appreciated but I have already mentioned that I don't want to use graphic elements. The font itself will create the barcode from the encoded text.

    I've had some success in this area. The reason why the encoded text wasn't working in InDesign was because "use typographers quotes" was selected in the preferences. Therefore, when I pasted the encoded text, InDesign converted the standard straight quotes to "curly" quotes.

    Now that this has been resolved, I can move forward :).

    To test this, I used an online encoder : https://www.bcgen.com/fontencoder/
    This encoded the test number: 95141111020209 to: ÍÃ.++"")qÎ

    I then copied the text into InDesign and applied the Code 128 font: https://www.dafont.com/code-128.font

    For independent verification I used this online barcode generator: https://barcode.tec-it.com/en/Code128?data=95141113020207 and using the original number, created and downloaded the barcode.

    Finally, I compared the results:
    -image-

    The red barcode is the font version and the black one is from the online generator. As far as I'm concerned, within tolerances, this is an exact match :)

    So now for the hard part. ie. COMPUTING THE CHECKSUM from the explanation in this link: http://www.barcodeisland.com/code128.phtml

    What isn't clear is it says: Take the value of the start character (103, 104, or 105) and make that the starting value of the running checksum. The example uses 103. I think Code 128 has 3 different flavours: A (auto) and B and C, so perhaps that's what is referred to.

    The "weighting" reminds me of MSB and LSB in binary. I have to set a few hours aside and carefully look at this and then start to construct some code to do the conversion. Bit difficult at the moment.

    [EDIT] This might sound a bit rude, but if someone has another way of generating the sample barcode and checking it, that would be very helpful. I'd rather rely on this forum than unknowns on the web. Cheers.

  20. Julian S

    25 Jan 2018 Pre-Release Testers, Xojo Pro UK
    Edited last year

    Steve, give this a quick try:

    https://grandzebu.net/informatique/codbar/code128.ttf

    with:

    ÒÈ.++"")qÓ (corrected code, you posted a number ending in 7 for that url but your main test is ending in 9)

    95141111020209

    If that works for you there's VB source that should be easy to convert to Xojo.

    https://grandzebu.net/informatique/codbar-en/code128.htm

  21. Newer ›

or Sign Up to reply!