Jump to content

Code 39

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 200.180.20.4 (talk) at 18:38, 9 June 2008. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A Code 39 Barcode Label
"WIKIPEDIA" encoded in Code 39

Code 39 (also known as "USS Code 39", "Code 3/9", "Code 3 of 9", "USD-3", "Alpha39", "Type 39") is a barcode symbology that can encode uppercase letters (A through Z), digits (0 through 9) and a handful of special characters like the $ sign. The barcode itself does not contain a check digit (in contrast to—for instance—Code 128), but it can be considered self-checking by some, on the grounds that a single erroneously interpreted bar cannot generate another valid character. Possibly the most serious drawback of Code 39 is its low data density: It requires more space to encode data in Code 39 than, for example, in Code 128. This means that very small goods cannot be labeled with a Code 39 based barcode. However, Code 39 is still widely used and can be decoded with virtually any barcode reader.

The name Code 39 is derived from the fact that three of the nine elements that constitute a codeword are wide elements, the remaining six are narrow. Code 39 was developed by Dr. David Allais and Ray Stevens of Intermec in 1974. It was later standardised as ANSI MH 10.8 M-1983 and MIL-STD-1189.

The width ratio between narrow and wide can be chosen between 1:2 and 1:3.

Encoding

The * character presented below is not a true encodable character, but is the start and stop 'symbol' for Code 39. The asymmetry of the symbol allows the reader to determine the direction of the barcode being scanned. This code is traditionally mapped to the * character in barcode fonts and will often appear with the human-readable representation alongside the barcode.

These tables outline the 3 of 9 specification:

Legend
Format1Format2Desc
WBWide - Black
NbNarrow - Black
wWWide - White
nwNarrow - White


Code Details
CharFormat1Format2
*NwNnWnWnN bWbwBwBwb
-NwNnNnWnW bWbwbwBwB
$NwNwNwNnN bWbWbWbwb
%NnNwNwNwN bwbWbWbWb
(space)NwWnNnWnN bWBwbwBwb
.WwNnNnWnN BWbwbwBwb
/NwNwNnNwN bWbWbwbWb
+NwNnNwNwN bWbwbWbWb
0NnNwWnWnN bwbWBwBwb
1WnNwNnNnW BwbWbwbwB
2NnWwNnNnW bwBWbwbwB
3WnWwNnNnN BwBWbwbwb
4NnNwWnNnW bwbWBwbwB
5WnNwWnNnN BwbWBwbwb
6NnWwWnNnN bwBWBwbwb
7NnNwNnWnW bwbWbwBwB
8WnNwNnWnN BwbWbwBwb
9NnWwNnWnN bwBWbwBwb
AWnNnNwNnW BwbwbWbwB
BNnWnNwNnW bwBwbWbwB
CWnWnNwNnN BwBwbWbwb
DNnNnWwNnW bwbwBWbwB
EWnNnWwNnN BwbwBWbwb
FNnWnWwNnN bwBwBWbwb
GNnNnNwWnW bwbwbWBwB
HWnNnNwWnN BwbwbWBwb
INnWnNwWnN bwBwbWBwb
JNnNnWwWnN bwbwBWBwb
KWnNnNnNwW BwbwbwbWB
LNnWnNnNwW bwBwbwbWB
MWnWnNnNwN BwBwbwbWb
NNnNnWnNwW bwbwBwbWB
OWnNnWnNwN BwbwBwbWb
PNnWnWnNwN bwBwBwbWb
QNnNnNnWwW bwbwbwBWB
RWnNnNnWwN BwbwbwBWb
SNnWnNnWwN bwBwbwBWb
TNnNnWnWwN bwbwBwBWb
UWwNnNnNnW BWbwbwbwB
VNwWnNnNnW bWBwbwbwB
WWwWnNnNnN BWBwbwbwb
XNwNnWnNnW bWbwBwbwB
YWwNnWnNnN BWbwBwbwb
ZNwWnWnNnN bWBwBwbwb


Please Note: In between each character (the start and stop characters included). There is a thin space (shown as w below). For example, if you wanted a Code 39 barcode composed of the letter "A", you would need the following to be encoded: "*A*". [bWbwBwBwb]w[BwbwbWbwB]w[bWbwBwBwb]
The code will not be read without these spaces.

Code 39 mod 43

Code 39 is sometimes, though rarely, used with an optional modulo 43 check digit. Using it requires this feature to be enabled in the barcode reader. The code with check digit is referred to as Code 39 mod 43.

Here is how to do the checksum calculation:

  • Take the value (0 through 42) of each character in the barcode excluding start and stop codes.
  • Sum the values.
  • Divide the result by 43.
  • The remainder is the value of the checksum character to be appended.

This can be expressed in Visual Basic as:

Const charSet As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"
Function Mod43(C39 As String) As String
  For i = 1 To Len(C39)
    total = InStr(charSet, Mid(C39, i, 1)) - 1 + total
  Next i
  Mod43 = C39 & Mid$(charSet, (total Mod 43 + 1), 1)
End Function

or in Java as (with checking for invalid characters):

public static final String charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
public static char getChecksum(String barCode) throws Exception
{
  int total = 0;
  CharacterIterator it = new StringCharacterIterator(barCode);
  for (char ch = it.current(); ch != CharacterIterator.DONE; ch = it.next())
  {
    int charValue = charSet.indexOf(ch);
    if (charValue == -1)
    {
      // Invalid character.
      throw new Exception("Input String '" +barCode+ "' contains characters that are invalid in a Code39 barcode.");
    }
    total += charValue;
  }
  int checksum = total % 43;
  return charSet.charAt(checksum);
}

Full ASCII Code 39

Code 39 is restricted to 44 characters. In Full ASCII Code 39 Symbols 0-9, A-Z, "." ,and "-" are the same as their representations in Code 39. Lower case letters, additional punctuation characters and control characters are represented by sequences of two characters of Code 39.

Code Details
NrCharacterEncoding    NrCharacterEncoding    NrCharacterEncoding    NrCharacterEncoding
0NUL%U 32[space][space] 64@%V 96`%W
1SOH$A 33!/A 65AA 97a+A
2STX$B 34"/B 66BB 98b+B
3ETX$C 35#/C 67CC 99c+C
4EOT$D 36$/D 68DD 100d+D
5ENQ$E 37%/E 69EE 101e+E
6ACK$F 38&/F 70FF 102f+F
7BEL$G 39'/G 71GG 103g+G
8BS$H 40(/H 72HH 104h+H
9HT$I 41)/I 73II 105i+I
10LF$J 42*/J 74JJ 106j+J
11VT$K 43+/K 75KK 107k+K
12FF$L 44,/L 76LL 108l+L
13CR$M 45-- 77MM 109m+M
14SO$N 46.. 78NN 110n+N
15SI$O 47//O 79OO 111o+O
16DLE$P 4800 80PP 112p+P
17DC1$Q 4911 81QQ 113q+Q
18DC2$R 5022 82RR 114r+R
19DC3$S 5133 83SS 115s+S
20DC4$T 5244 84TT 116t+T
21NAK$U 5355 85UU 117u+U
22SYN$V 5466 86VV 118v+V
23ETB$W 5577 87WW 119w+W
24CAN$X 5688 88XX 120x+X
25EM$Y 5799 89YY 121y+Y
26SUB$Z 58:/Z 90ZZ 122z+Z
27ESC%A 59;%F 91[%K 123{%P
28FS%B 60<%G 92\%L 124|%Q
29GS%C 61=%H 93]%M 125}%R
30RS%D 62>%I 94^%N 126~%S
31US%E 63?%J 95_%O 127DEL%T, %X, %Y, %Z