Interleaved 2 of 5, or ITF, encoding is a widely used barcode pattern across many industries, usually for shipping and inventory control. Its advantages over other barcodes are high information density and readability even on poor printing surfaces (like rough corrugated cardboard). Its chief disadvantage is that it will encode an even number of numerical characters only, it cannot encode letters or punctuation. Odd numbers of digits will usually be padded to even by prepending an extra 0. As a variable-width barcode, there is no theoretical limit to the length of the number encoded, but in a practical sense scanners may have difficulty reading extremely long codes, and 14 digits is very common.

The name Interleaved 2 of 5 comes from the two encoding methods that define the standard. Each digit is encoded as 5 bars, 2 of which will be wide. Digits are encoded as pairs, interleaved together, meaning the odd digits are encoded as five black bars each, and the even digits are encoded as the five white spaces between the black bars.

Like the common UPC-A and EAN barcodes, ITF is a one-dimensional barcode made of alternating black and white lines of variable width, along with a start and stop code at the ends. The main differences are that ITF bars only come in two widths (UPC uses four widths), each set of bars encodes 2 digits at a time, and the checksum for error checking is not mandatory. The width of the wide bars is specified as 2-3 times the width of the narrow bars.

The following demonstrates how to encode the number 108 in Interleaved 2 of 5.

First we must make sure the number of digits is even, since ITF encodes digits in pairs. The usual solution to odd digits is to pad the number by adding an extra 0 to the front, so 108 becomes 0108.

Next we need to look up each digit in the encoding table, which can be found below. We have four digits, which are encoded nnWWn (0), WnnnW (1), nnWWn (0), and WnnWn (8) where n is a narrow bar and W is a wide bar.

Now we interleave each pair of digits like so:

0 : n n W W n 0 : n n W W n
1 : W n n n W 8 : W n n W n
01: nWnnWnWnnW 08: nWnnWnWWnn

Finally, we add the start code (nnnn) and end code (Wnn)

nnnnnWnnWnWnnWnWnnWnWWnnWnn

Showing the encoded characters including strt and end:

s r 0 0 0 0 0 0 0 0 0 0 e d (black bars)
nnnn nWnnWnWnnW nWnnWnWWnn Wnn
t t 1 1 1 1 1 8 8 8 8 8 n (white bars)

Each alternating bar is black-white-black-white, so the resulting bar code would look something like this:

# # # # ## ## # # # ## ## # ## #
# # # # ## ## # # # ## ## # ## #
# # # # ## ## # # # ## ## # ## #

Note that each number is encoded as 3 narrow and 2 wide bars.

Encoding table:

0 | nnWWn |

1 | WnnnW |

2 | nWnnW |

3 | WWnnn |

4 | nnWnW |

5 | WnWnn |

6 | nWWnn |

7 | nnnWW |

8 | WnnWn |

9 | nWnWn |

start | nnnn |

end | Wnn |

The following is a JavaScript implementation of ITF which takes a string of numbers and encodes them as a series of n and W characters representing Interleaved 2 of 5 format.

function barcodeEncodeITF(myNumber) {
"use strict";
// ensure we have a string rather than a number
var x = String(myNumber);
// prepend a 0 if we have an odd number of digits
if (x.length%2==1) { x = '0'+x; }
// encoding table for each digit 0-9
var encoding = ['nnWWn'
,'WnnnW'
,'nWnnW'
,'WWnnn'
,'nnWnW'
,'WnWnn'
,'nWWnn'
,'nnnWW'
,'WnnWn'
,'nWnWn'
];
// start and end codes
var start = 'nnnn';
var end = 'Wnn';
// start building the encoded string
var encoded = start;
// Interleaved 2 of 5 encodes 2 digits at a time
var first = 0;
var second = 0;
for (var i=0; i<x.length; i+=2) {
// get the encoding for two digits at a time
first = encoding[Number(x[i])];
second = encoding[Number(x[i+1])];
// interleave the two encodings
for (var j=0; j<5; j++) {
encoded += first[j] + second[j];
}
}
encoded += end;
return(encoded);
}