TIP: Function to calculate check-digit for GTIN barcode

In serialization projects we sometimes deploy the GTIN (Global Trade Item Number) standard as set out by the GS1 organization. This number is used by an organization to uniquely identify a trade item or product / service. an sGTIN (serialized GTIN) is a unique identifier for an INSTANCE of a particular product identified by the GTIN number.

There are four GTIN formats. GTIN-8 (length 8), GTIN-12, GTIN-13, GTIN-14.

For applications that require a uniform 14-digit format, leading zeroes need to be added:

  • GTIN-8: 000000nnnnnnnn

  • GTIN-12: 00nnnnnnnnnnnn

  • GTIN-13: 0nnnnnnnnnnnnn

  • GTIN-14: nnnnnnnnnnnnnn <<< No need to change since it's already 14 in length

The check-digit always makes up the last character of the barcode number and is used to verify whether the barcode number is valid or not (on reading).

Here is a function module that can be used to create this check-digit. Note: You can also adjust it to check a check-digit is valid too...


Take the Barcode number in as input

Import Strcuture

The export structure will have the check digit and the GTIN number

Export check digit and GTIN

Exceptions are captured for invalid barcode formats

The Function Module code would look something like this...

FUNCTION z_fm_em_calculate_bc_check_dig.

CONSTANTS: lc_numeric TYPE string VALUE '0123456789'.

DATA: lv_len TYPE i,

lv_pos1 TYPE i,

lv_pos2 TYPE i,

lv_pos3 TYPE i,

lv_pos4 TYPE i,

lv_pos5 TYPE i,

lv_pos6 TYPE i,

lv_pos7 TYPE i,

lv_pos8 TYPE i,

lv_pos9 TYPE i,

lv_pos10 TYPE i,

lv_pos11 TYPE i,

lv_step1 TYPE i,

lv_step2 TYPE i,

lv_step3 TYPE i,

lv_step4 TYPE i,

lv_step5 TYPE i,

lv_cd(1) TYPE c.

ev_return = 0.

IF iv_bc CO '0123456789'.

lv_len = strlen( iv_bc ).

CASE lv_len.

WHEN 11. "GTIN-12

lv_pos1 = iv_bc(1).

lv_pos2 = iv_bc+1(1).

lv_pos3 = iv_bc+2(1).

lv_pos4 = iv_bc+3(1).

lv_pos5 = iv_bc+4(1).

lv_pos6 = iv_bc+5(1).

lv_pos7 = iv_bc+6(1).

lv_pos8 = iv_bc+7(1).

lv_pos9 = iv_bc+8(1).

lv_pos10 = iv_bc+9(1).

lv_pos11 = iv_bc+10(1).

lv_step1 = lv_pos1 + lv_pos3 + lv_pos5 + lv_pos7 + lv_pos9 + lv_pos11.

lv_step2 = lv_step1 * 3.

lv_step3 = lv_pos2 + lv_pos4 + lv_pos6 + lv_pos8 + lv_pos10.

lv_step4 = lv_step2 + lv_step3.

lv_step5 = 10 * ( ( lv_step4 DIV 10 ) + 1 ).

ev_check_digit = lv_step5 - lv_step4. "Set the Check Digit

lv_cd = ev_check_digit.

CONCATENATE iv_bc lv_cd INTO ev_gtin. "Set the GTIN


"Issue error ev_return

ev_return = 4.

RAISE unsupported_length.


ELSE. "Non-numeric barcode

ev_return = 8.

RAISE non_numeric.



232 views0 comments