Skip to main content

ZPL how-to

GS1-128 barcodes in ZPL, with the check digit

GS1-128 is Code 128 with a special character, FNC1, straight after the start. ZPL prints it with ^BC. The parts that go wrong are the check digit inside a GTIN or SSCC, which is yours to get right, and the FNC1 that has to follow a field whose length varies.

FNC1 first: what makes it GS1-128

A GS1-128 barcode is a start character, FNC1, the data, a check character and a stop. The data is element strings: an Application Identifier (AI) such as 01 for a GTIN or 17 for an expiry date, then its data. In ZPL, >; starts subset C (digits in pairs) and >8 is FNC1:

zpl
^XA
^BY2^FO40,40^BCN,100,Y,N,N^FD>;>8010950110153000317261231^FS
^XZ
  • This is (01) GTIN 09501101530003 and (17) expiry 31 December 2026. A scanner read it as GS1, (01)09501101530003(17)261231, with the symbology identifier ]C1, which is how a scanner reports GS1-128.
  • The text line under it printed the digits only, 010950110153000317261231, with no parentheses. Turn it off (^BCN,100,N) and print your own text field if you want them.
  • Without the FNC1 it is a plain Code 128 barcode. The same GTIN in automatic mode, ^BCN,100,Y,N,N,A^FD0109501101530003^FS, scanned as 0109501101530003 with ]C0: the right digits, but not GS1, so software that expects GS1 will not read it as a GTIN.

Mode D: AIs in parentheses

The last value of ^BC, the mode, can be D, which Zebra's reference calls UCC/EAN mode (firmware x.11 and later). It starts with FNC1 for you and takes the AIs in parentheses:

zpl
^XA
^BY2^FO40,40^BCN,100,Y,N,N,D^FD(01)09501101530003(17)261231^FS
^XZ
  • The parentheses print in the text line, (01)09501101530003(17)261231, and are left out of the bars: in Labelary the bars were the same, dot for dot, as the >;>8 sample above.
  • That is what GS1 asks of the text: AIs in parentheses, which are never encoded (GS1 General Specifications, release 26.0, section 4.14). FNC1 is never shown in it.
  • Mode D also sizes the text line itself, to the width of the bars.

The check digit: mod 10

A GTIN, an SSCC and a GLN end in a check digit, and it is part of the data: a GTIN-14 is 13 digits and a check digit, an SSCC 17 and one. GS1's rule (GS1 General Specifications, release 26.0, section 7.9.1):

  1. Take the digits without the check digit. From the right, multiply them by 3, 1, 3, 1 and so on: the rightmost by 3.
  2. Add the results.
  3. The check digit is what takes the sum up to the next multiple of 10 (0 if it is one already).

For the GTIN above, without its check digit:

text
Digit    0  9   5  0  1  1  0  1   5  3  0  0  0
Weight   3  1   3  1  3  1  3  1   3  1  3  1  3
Product  0  9  15  0  3  1  0  1  15  3  0  0  0

The products add up to 47, so the check digit is 3, and the GTIN is 09501101530003. In Python:

python
def gs1_check_digit(digits: str) -> str:
    # From the right: the last digit is weighted 3, the one before it 1, and so on.
    total = sum(int(d) * (3 if i % 2 == 0 else 1) for i, d in enumerate(reversed(digits)))
    return str((10 - total % 10) % 10)


print(gs1_check_digit('0950110153000'))      # GTIN-14 without its check digit
print(gs1_check_digit('00950110153000000'))  # SSCC without its check digit

It printed 3 and 7. With the 7, the SSCC is 009501101530000007, printed in mode D like this:

zpl
^XA
^BY2^FO40,40^BCN,100,Y,N,N,D^FD(00)009501101530000007^FS
^XZ

It scanned as (00)009501101530000007.

Does the printer work it out?

Zebra's reference says mode D "automatically determines if a check digit is required, calculates it, and prints it". Its SSCC example sends all 18 digits, the last a placeholder. Labelary did it only when there was a digit to replace:

  • (00)009501101530000000, a 0 in place of the check digit, scanned as (00)009501101530000007. Labelary changed the digit with no error; only its linter said so: Check digit '0' at index 21 does not match calculated check digit '7'; used '7' instead.
  • A GTIN with a wrong check digit, (01)09501101530000, was changed the same way, to (01)09501101530003.
  • Leave the digit off and nothing is added. (00)00950110153000000, 17 digits, scanned as the 19 digits 0000950110153000000: no check digit, and too short to be an SSCC. (01)0950110153000 scanned as 010950110153000, 13 digits.
  • The fifth value of ^BC, "UCC check digit", changed nothing in mode D: ^BCN,100,Y,N,Y,D^FD(00)009501101530000007^FS drew the same bars as the sample above. In mode N, Zebra's example sends >;>8, 00 and 17 digits with it on, for the printer to add the 20th. In Labelary, ^BCN,100,Y,N,Y,N^FD>;>80000950110153000000^FS scanned as 000095011015300000: the odd last digit was dropped (Ignored invalid character '0' at index 22) and no check digit was added.
  • Mode U, Zebra's UCC case mode for an SSCC, takes 19 digits, the 00 and 17 more, and adds the check digit: ^BCN,100,Y,N,N,U^FD0000950110153000000^FS scanned as (00)009501101530000007.
  • Zebra says mode U drops digits past 19 and pads shorter data with zeros on the right, so a wrong length makes a different SSCC with no error. Labelary padded on the left instead: 18 digits, ^BCN,100,Y,N,N,U^FD009501101530000000^FS, scanned as (00)095011015300000003, with only a linter warning: Value '009501101530000000' is not valid for this barcode; used '0009501101530000000' instead.

A printer and a preview may not agree on any of this, and a silently replaced digit hides a mistake in your data. Work the check digit out yourself and send every digit. Then whatever the printer does, the barcode carries the number you meant.

Fields that vary in length need FNC1 after them

A scanner finds where a field ends from its AI. Some AIs have a predefined length, so nothing follows them: those starting 00, 01, 02, 03, 04, 11 to 20, 31 to 36 and 41 (GS1 General Specifications, release 26.0, table 7-6). Every other field must be followed by FNC1 when another field comes after it, even one whose data is a fixed length:

zpl
^XA
^BY2^FO40,40^BCN,100,Y,N,N,D^FD(01)09501101530003(10)ABC123>8(17)261231^FS
^XZ
  • (10) is a batch or lot, 1 to 20 characters, so >8 ends it. It scanned as (01)09501101530003(10)ABC123(17)261231; the data a scanner sends is 010950110153000310ABC123<GS>17261231, where <GS> is the FNC1. Mode D adds FNC1 at the start only: the ones in between are yours.
  • Without the >8, (01)09501101530003(10)ABC123(17)261231 scanned as (01)09501101530003(10)ABC12317261231: the expiry date became part of the lot, with no error.
  • The last field needs no FNC1. (01)09501101530003(17)261231(10)ABC123, with the lot last, scanned as (01)09501101530003(17)261231(10)ABC123. GS1 advises putting predefined-length fields first (section 7.8.6.3), which saves the FNC1.
  • A GS1-128 barcode holds at most 48 characters, counting the AIs and each FNC1 between fields, but not the parentheses or the FNC1 at the start (section 5.4.4.3).

The check character at the end: the printer adds it

Every Code 128 barcode, GS1-128 included, ends with a symbol check character before the stop. The printer always adds it, and ^BC has no setting for it. It is not printed in the text line. Leave it out of your data: it is not the mod 10 digit above. GS1's worked example (section 5.4.7.5.1, table 5-27) is the lot 2503X:

zpl
^XA
^BY2^FO40,40^BCN,100,Y,N,N,D^FD(10)2503X^FS
^XZ

Each character's value is multiplied by its position, the start and the first character both counting 1:

text
Character  Start C  FNC1  10  25  03  Code B    X
Value          105   102  10  25   3     100   56
Weight           1     1   2   3   4       5    6
Product        105   102  20  75  12     500  336

The products add up to 1150, and 1150 divided by 103 leaves 17: the check character. Read back from Labelary's bars, the character it drew was 17.

GS1-128 with zeepler

Send this body to POST https://api.zeepler.com/v1/zeeplify with your API key in the x-api-key header. It is a working request, not an illustration: on every change, our tests run this same file through the API's code and compare the result with the ZPL below.

json
1{
2 "config": {
3 "size": "4x2"
4 },
5 "data": {
6 "sscc": "(00)00950110153000000",
7 "item": "(01)0950110153000(10)ABC123(17)261231"
8 },
9 "templates": [
10 {
11 "type": "BarCode",
12 "dataPath": "sscc",
13 "coords": [40, 30],
14 "style": "gs1-128",
15 "height": 80
16 },
17 {
18 "type": "BarCode",
19 "dataPath": "item",
20 "coords": [40, 220],
21 "style": "gs1-128",
22 "height": 80
23 }
24 ]
25}

And the ZPL that comes back, ready to send to the printer:

zpl
^XA^MCY^XZ
    ^XA
    ^SZ2
    ^MMT
    ^MNY
    ^LL406
    ^PW812
    ^LH0,0
^BY2,2,59^FO40,30^BCN,80,Y,N,N,D^FD(00)009501101530000007^FS
^BY2,2,59^FO40,220^BCN,80,Y,N,N,D^FD(01)09501101530003(10)ABC123>8(17)261231^FS
^XZ

zeepler's BarCode element has a gs1-128 style. Send the data as it is written in the text line, AIs in parentheses:

  • A GTIN, SSCC, GLN or GSIN sent without its check digit gets one: the request's SSCC is 17 digits and the GTIN 13, and the ZPL carries 009501101530000007 and 09501101530003. A wrong check digit is refused with a 400, not changed.
  • It puts >8 after each field that is not of predefined length, unless it is last, and prints with mode D. Fields stay in the order you send them.
  • It checks each field against its AI: length, digits, YYMMDD dates and GS1's character set. It takes the AIs a shipping, healthcare or asset label usually carries, listed in the BarCode style's description in openapi.json, and refuses any other with a 400, as it does a value over 48 characters.
  • The barcode must be left-aligned: align center and right are refused, because zeepler does not work out a mode D barcode's width.
  • In Labelary the two barcodes scanned as (00)009501101530000007 and (01)09501101530003(10)ABC123(17)261231, both ]C1.

The Code 128 guide covers ^BY, widths and subsets.

Sending it

  • Call the API from your own server or integration engine, not from a web page. An API key in a browser is a key anyone can copy.
  • A successful call answers 200 with the ZPL as plain text. A request the API cannot turn into a label answers 400 with every problem listed by field, and is not billed.
  • Send the ZPL to a Zebra or other ZPL printer at 203 dpi. At 300 dpi the same label prints at two-thirds of the size.

The API reference has every field and status code.

Before you go live

  • Scan a printed label with a scanner set to report GS1, and check it sends ]C1 and the fields you meant, each in its own place.
  • Leave at least 10 modules of blank space each side of the bars, 20 dots at ^BY2, as GS1 asks (GS1 General Specifications, release 26.0, section 5.4.4.2).
  • Text does not wrap. A value longer than the space you left runs off the edge of the label, so set maxLength on any field whose length you do not control.
  • Coordinates are in dots from the top-left corner. A preview shows the layout; then print one label, measure it, and adjust, because a preview cannot show what your printer does with it.
  • Scan every barcode and QR code on a printed label before relying on it.

Related guides

  • QR code size in ZPL: How ^BQ magnification sets the size of a QR code, and how to work out the size in dots.
  • Printing ^, ~ and _ in ZPL: Why a caret or tilde in your data cuts the field short, and how ^FH prints them.
  • Font size in ZPL: Setting text height with ^A and ^CF, converting points to dots, and why ^CF carries over.
  • Send ZPL to a Zebra printer from C#: Send ZPL from .NET to a network printer on port 9100, or to a USB printer through the Windows spooler, without the driver redrawing it.
  • Code 128 barcodes in ZPL: Set a Code 128 barcode's width and height with ^BY and ^BC, work out its width in dots, and pick subsets A, B and C.
  • Accented characters in ZPL (^CI28): Print é, ç, ô and € with ^CI28, switch the printer back with ^CI0, and what to do on older firmware.
  • Send ZPL to a Zebra printer from Python: Send ZPL from Python to a network printer on port 9100, or to a USB printer through the Windows spooler, without the driver redrawing it.
  • Change a barcode to a QR code in ZPL: Swap ^BC for ^BQ, start the field data with QA, so the first characters scan, and reset the ^BY left over from the barcode.
  • Send ZPL to a Zebra printer from Node.js: Send ZPL from Node.js to a network printer on port 9100 or to a USB printer through a Windows printer share, and print from a web page through your server.
  • Label size and origin in ZPL (^PW, ^LL, ^LH): Set the label size in dots with ^PW and ^LL, move 0,0 with ^LH, and stop the last label's settings moving this one.
  • Templates in ZPL: store a format with ^DF, fill it with ^XF: Store a label layout on the printer with ^DF and ^FN, fill it with ^XF, and what prints when a field or the format is missing.
  • Several labels in one ZPL job: Send several different labels in one job, stop each one inheriting the last one's font and barcode settings, and number a run with ^SN and ^PQ.
  • EAN-13 and Code 93 barcodes in ZPL: Print EAN-13 with ^BE and Code 93 with ^BA, and the data each one changes: a replaced check digit, and characters Code 93 cannot hold.
  • Print copies of a ZPL label with ^PQ: Print several copies of one label with ^PQ, why ^PQ after ^XZ prints only one, and each serial number twice with ^SN.
  • Center text in ZPL with ^FB: Center text of any length with ^FB, why centered text sits a little left, and text too long for the block.
  • Center or right-align a barcode in ZPL: Right-align a barcode with the third value of ^FO, center one by working out its width, and why ^FB does not move it.
  • Wrap text and new lines in ZPL: Wrap long text with ^FB, start a new line with \&, and stop ^FB writing over its last line or cutting text off.
  • Rotate text and barcodes in ZPL: Turn text and barcodes with N, R, I and B, where ^FO and ^FT put a turned field, ^FW and ^POI, and why both stay set.
  • Preview ZPL without a printer: See a label before printing it with Labelary's viewer or API, what zeepler's playground shows, and what no preview can tell you.
  • Boxes, lines, underline and bold in ZPL (^GB): Draw boxes and lines with ^GB, print white on black with ^FR, underline text with a line, and make text bolder.
  • Logos and images in ZPL (^GF, ~DG, ^XG): Print a logo with ^GF, turn a PNG into hex, keep it under the size limit, and store it once with ~DG to print with ^XG.
  • 300 dpi printers, and the same ZPL on 203 and 300 dpi: Why a 203 dpi label prints smaller at 300 dpi, scaling it by 1.5 and what does not scale exactly, ^MU in millimetres, and reading the printer's resolution.
  • Comments in ZPL: ^FX: Write a comment with ^FX, why a caret inside one runs as a command, comments outside ^XA and ^XZ, and why // is not a comment.
  • Emulate a Zebra printer for testing: A stand-in printer on port 9100 in a few lines of Python or Node.js, pointing your send code at it, checking what it caught in Labelary, and what a stand-in cannot test.
  • Convert ZPL to PDF or PNG: Labelary's API turns ZPL into a PDF, one label to a page, or a PNG of one label: curl, Python and Node.js, its limits, and what it means to send it your labels.
  • Convert a PDF to ZPL: Draw each page of the PDF as dots at the printer's resolution and send them as ^GF: a Python script, sizing, grey, the ^GF size limit, and why the label prints as a picture.
  • Convert EPL to ZPL: Translate an EPL2 label to ZPL by hand: each common EPL command and its ZPL equivalent, one label translated line by line, what does not carry over, and switching a printer's language.
  • Convert HTML to ZPL: Two ways from an HTML label to ZPL: draw the page in a headless browser and send the dots as ^GF (a Node.js script), or write the label as ZPL fields, with which HTML and CSS ideas carry over and which do not.

Help with an integration

Email info@zeepler.com. You will get a reply within 2 business days. The support policy covers response times and version changes.