Introduction

A recent project at work required that I write out barcode characters and read them. This experience inspired me to start a side project writing some code that renders a barcode on the Windows screen, given the proper input. This is the result of that project

Code 128 basics

Code 128 is a very high density alpha-numeric code that was introduced in 1981 and is being used in a variety of applications. Code 128 characters consist of 3 bars and 3 spaces, which are built by using 11 modules each of which can be black or white. Because all of the characters are built from the same 11 modules, there really is only one element width that needs to be defined, the module width. The bar/space patterns for the 107 data characters and the start/stop codes are listed below. Each pattern is 11 characters (modules) long, and is either 'b' (that module is a bar) or 's' (that module is a space). Note that there are three subsets to Code 128, Subset A and B cover the ASCII character set, and Subset C is a double-density numeric-only subset. Three different start characters tell the barcode reader which subset is the starting subset, and three shift characters allow changing subsets within a Code 128 barcode.

Code A

Code B

Code C

Value

Pattern

Space

Space

00

0

bbsbbssbbss

!

!

01

1

bbssbbsbbss

02

2

bbssbbssbbs

#

#

03

3

bssbssbbsss

$

$

04

4

bssbsssbbss

%

%

05

5

bsssbssbbss

&

&

06

6

bssbbssbsss

'

(

07

7

bssbbsssbss

(

)

08

8

bsssbbssbss

)

*

09

9

bbssbssbsss

*

*

10

10

bbssbsssbss

+

+

11

11

bbsssbssbss

,

,

12

12

bsbbssbbbss

-

-

13

13

bssbbsbbbss

.

.

14

14

bssbbssbbbs

/

/

15

15

bsbbbssbbss

0

0

16

16

bssbbbsbbss

1

1

17

17

bssbbbssbbs

2

2

18

18

bbssbbbssbs

3

3

19

19

bbssbsbbbss

4

4

20

20

bbssbssbbbs

5

5

21

21

bbsbbbssbss

6

6

22

22

bbssbbbsbss

7

7

23

23

bbbsbbsbbbs

8

8

24

24

bbbsbssbbss

9

9

25

25

bbbssbsbbss

:

:

26

26

bbbssbssbbs

;

;

27

27

bbbsbbssbss

<

<

28

28

bbbssbbsbss

Equal

Equal

29

29

bbbssbbssbs

>

>

30

30

bbsbbsbbsss

?

?

31

31

bbsbbsssbbs

@

@

32

32

bbsssbbsbbs

A

A

33

33

bsbsssbbsss

B

B

34

34

bsssbsbbsss

C

C

35

35

bsssbsssbbs

D

D

36

36

bsbbsssbsss

E

E

37

37

bsssbbsbsss

F

F

38

38

bsssbbsssbs

G

G

39

39

bbsbsssbsss

H

H

40

40

bbsssbsbsss

I

I

41

41

bbsssbsssbs

J

J

42

42

bsbbsbbbsss

K

K

43

43

bsbbsssbbbs

L

L

44

44

bsssbbsbbbs

M

M

45

45

bsbbbsbbsss

N

N

46

46

bsbbbsssbbs

O

O

47

47

bsssbbbsbbs

P

P

48

48

bbbsbbbsbbs

Q

Q

49

49

bbsbsssbbbs

R

R

50

50

bbsssbsbbbs

S

S

51

51

bbsbbbsbsss

T

T

52

52

bbsbbbsssbs

U

U

53

53

bbsbbbsbbbs

V

V

54

54

bbbsbsbbsss

W

W

55

55

bbbsbsssbbs

X

X

56

56

bbbsssbsbbs

Y

Y

57

57

bbbsbbsbsss

Z

Z

58

58

bbbsbbsssbs

[

[

59

59

bbbsssbbsbs

\

\

60

60

bbbsbbbbsbs

]

]

61

61

bbssbssssbs

^

^

62

62

bbbbsssbsbs

_

_

63

63

bsbssbbssss

NUL

`

64

64

bsbssssbbss

SOH

a

65

65

bssbsbbssss

STX

b

66

66

bssbssssbbs

ETX

c

67

67

bssssbsbbss

EOT

d

68

68

bssssbssbbs

ENQ

e

69

69

bsbbssbssss

ACK

f

70

70

bsbbssssbss

BEL

g

71

71

bssbbsbssss

BS

h

72

72

bssbbssssbs

HT

i

73

73

bssssbbsbss

LF

j

74

74

bssssbbssbs

VT

k

75

75

bbssssbssbs

FF

l

76

76

bbssbsbssss

CR

m

77

77

bbbbsbbbsbs

SO

n

78

78

bbssssbsbss

SI

o

79

79

bsssbbbbsbs

DLE

p

80

80

bsbssbbbbss

DC1

q

81

81

bssbsbbbbss

DC2

r

82

82

bssbssbbbbs

DC3

s

83

83

bsbbbbssbss

DC4

t

84

84

bssbbbbsbss

NAK

u

85

85

bssbbbbssbs

SYN

v

86

86

bbbbsbssbss

ETB

w

87

87

bbbbssbsbss

CAN

x

88

88

bbbbssbssbs

EM

y

89

89

bbsbbsbbbbs

SUB

z

90

90

bbsbbbbsbbs

ESC

{

91

91

bbbbsbbsbbs

FS

|

92

92

bsbsbbbbsss

GS

}

93

93

bsbsssbbbbs

RS

~

94

94

bsssbsbbbbs

US

DEL

95

95

bsbbbbsbsss

FNC3

FNC3

96

96

bsbbbbsssbs

FNC2

FNC2

97

97

bbbbsbsbsss

Shift

Shift

98

98

bbbbsbsssbs

Switch Code C

Switch Code C

99

99

bsbbbsbbbbs

Switch Code B

FNC4

Switch Code B

100

bsbbbbsbbbs

FNC4

Switch Code A

Switch Code A

101

bbbsbsbbbbs

FNC1

FNC1

FNC1

102

bbbbsbsbbbs

START Code A

START Code A

START Code A

103

bbsbsbbbbss

START Code B

START Code B

START Code B

104

bbsbssbssss

START Code C

START Code C

START Code C

105

bbsbssbbbss

STOP

STOP

STOP

106

bbsssbbbsbsbb

Each Code 128 barcode has a check digit that immediately precedes the stop character. The check digit is a weighted sum of the data characters, modulus 103. The data characters are weight from left to right by the infinite sequence {1,2,3,...} An example of the check digit calculations is shown below, using the message "DATA".

Start A

D

A

T

A

Check Digit

Stop


36

33

52

33




1

2

3

4



To calculate the check digit, first find the sum of products: (36*1)+(33*2)+(52*3)+(33*4) = 390. Divide 390 by 103 to get 3 with a remainder of 81. The value of the check digit is 81 which corresponds to the ASCII character DC1. A picture of the entire barcode is shown below.

Note that the barcode reader uses the check digits to decode the barcode, but does not transmit them.

-Cheers Joy