Code 39
From Wikipedia, the free encyclopedia
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. One advantage of Code 39 is that since there is no need to generate a check digit, it can easily be integrated into existing printing system by adding a barcode font to the system or printer and then printing the raw data in that font.
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.
Contents |
[edit] 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 | ||
| Format1 | Format2 | Desc |
| W | B | Wide - Black |
| N | b | Narrow - Black |
| w | W | Wide - White |
| n | w | Narrow - White |
(Note, Format1 and Format2 are just two different ways of describing the same underlying encoding, not two variants of Code 39.)
| Code Details | ||
| Char | Format1 | Format2 |
| * | NwNnWnWnN | bWbwBwBwb |
| - | NwNnNnWnW | bWbwbwBwB |
| $ | NwNwNwNnN | bWbWbWbwb |
| % | NnNwNwNwN | bwbWbWbWb |
| (space) | NwWnNnWnN | bWBwbwBwb |
| . | WwNnNnWnN | BWbwbwBwb |
| / | NwNwNnNwN | bWbWbwbWb |
| + | NwNnNwNwN | bWbwbWbWb |
| 0 | NnNwWnWnN | bwbWBwBwb |
| 1 | WnNwNnNnW | BwbWbwbwB |
| 2 | NnWwNnNnW | bwBWbwbwB |
| 3 | WnWwNnNnN | BwBWbwbwb |
| 4 | NnNwWnNnW | bwbWBwbwB |
| 5 | WnNwWnNnN | BwbWBwbwb |
| 6 | NnWwWnNnN | bwBWBwbwb |
| 7 | NnNwNnWnW | bwbWbwBwB |
| 8 | WnNwNnWnN | BwbWbwBwb |
| 9 | NnWwNnWnN | bwBWbwBwb |
| A | WnNnNwNnW | BwbwbWbwB |
| B | NnWnNwNnW | bwBwbWbwB |
| C | WnWnNwNnN | BwBwbWbwb |
| D | NnNnWwNnW | bwbwBWbwB |
| E | WnNnWwNnN | BwbwBWbwb |
| F | NnWnWwNnN | bwBwBWbwb |
| G | NnNnNwWnW | bwbwbWBwB |
| H | WnNnNwWnN | BwbwbWBwb |
| I | NnWnNwWnN | bwBwbWBwb |
| J | NnNnWwWnN | bwbwBWBwb |
| K | WnNnNnNwW | BwbwbwbWB |
| L | NnWnNnNwW | bwBwbwbWB |
| M | WnWnNnNwN | BwBwbwbWb |
| N | NnNnWnNwW | bwbwBwbWB |
| O | WnNnWnNwN | BwbwBwbWb |
| P | NnWnWnNwN | bwBwBwbWb |
| Q | NnNnNnWwW | bwbwbwBWB |
| R | WnNnNnWwN | BwbwbwBWb |
| S | NnWnNnWwN | bwBwbwBWb |
| T | NnNnWnWwN | bwbwBwBWb |
| U | WwNnNnNnW | BWbwbwbwB |
| V | NwWnNnNnW | bWBwbwbwB |
| W | WwWnNnNnN | BWBwbwbwb |
| X | NwNnWnNnW | bWbwBwbwB |
| Y | WwNnWnNnN | BWbwBwbwb |
| Z | NwWnWnNnN | bWBwBwbwb |
Please Note: In between each character (the start and stop characters included) there is a thin space[citation needed] (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. Barcode fonts, however are likely to include this space within the glyph for the character.
[edit] Code 39 mod 43
Code 39 is sometimes 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 Python as:
def append_c39_checksum(st): charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%" return st + charset[ sum(charset.index(c) for c in st) % 43 ]
or 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 Visual Basic Script (with checking for invalid characters) as:
' compute checksum for Code 39, translated by antiguide ' paste the code in a .vbs file (MsWindos) dim charSet charset= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%" dim rejected ' string rejected="" dim total ' integer total=0 dim c39 c39=inputbox ("Code to generate: ") For i = 1 To Len(C39) dim rank rank= InStr(charSet, Mid(C39, i, 1)) - 1 if (rank >= 0) then total = total +rank else rejected=rejected & Mid(C39, i, 1) end if Next if rejected = "" then msgbox c39 & Mid(charSet, (total Mod 43 + 1), 1) else msgbox "wrong characters: " & rejected end if
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); }
or in C# as
public string ValidateMod43(string barcode) { int subtotal = 0; const string charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; for (int i = 0; i < barcode.Length; i++) { subtotal += charSet.IndexOf(barcode.Substring(i, 1)); } return charSet.Substring(subtotal%43, 1); }
or in Perl as
use feature 'state'; sub checksum { my $total = 0; state $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'; $total += index( $charset, $_ ) for split //, shift; return substr( $charset, $total % 43, 1 ); }
or in PHP as
function checksum( $string ) { $checksum = 0; $length = strlen( $string ); $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'; for( $i=0; $i < $length; ++$i ) { $checksum += strpos( $charset, $string[$i] ); } return substr( $charset, ($checksum % 43), 1 ); }
or in Tcl (with checking for invalid characters) as
proc checksum string { set checksum 0 set charset "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%" foreach char [split $string ""] { if {-1 == [set value [string first $char $charset]]} { return -code error "Wrong Code39 character:$char" } incr checksum $value } return [string index $charset [expr {$checksum % 43}]] }
[edit] 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 | ||||||||||||||
| Nr | Character | Encoding | Nr | Character | Encoding | Nr | Character | Encoding | Nr | Character | Encoding | |||
| 0 | NUL | %U | 32 | [space] | [space] | 64 | @ | %V | 96 | ` | %W | |||
| 1 | SOH | $A | 33 | ! | /A | 65 | A | A | 97 | a | +A | |||
| 2 | STX | $B | 34 | " | /B | 66 | B | B | 98 | b | +B | |||
| 3 | ETX | $C | 35 | # | /C | 67 | C | C | 99 | c | +C | |||
| 4 | EOT | $D | 36 | $ | /D | 68 | D | D | 100 | d | +D | |||
| 5 | ENQ | $E | 37 | % | /E | 69 | E | E | 101 | e | +E | |||
| 6 | ACK | $F | 38 | & | /F | 70 | F | F | 102 | f | +F | |||
| 7 | BEL | $G | 39 | ' | /G | 71 | G | G | 103 | g | +G | |||
| 8 | BS | $H | 40 | ( | /H | 72 | H | H | 104 | h | +H | |||
| 9 | HT | $I | 41 | ) | /I | 73 | I | I | 105 | i | +I | |||
| 10 | LF | $J | 42 | * | /J | 74 | J | J | 106 | j | +J | |||
| 11 | VT | $K | 43 | + | /K | 75 | K | K | 107 | k | +K | |||
| 12 | FF | $L | 44 | , | /L | 76 | L | L | 108 | l | +L | |||
| 13 | CR | $M | 45 | - | - | 77 | M | M | 109 | m | +M | |||
| 14 | SO | $N | 46 | . | . | 78 | N | N | 110 | n | +N | |||
| 15 | SI | $O | 47 | / | /O | 79 | O | O | 111 | o | +O | |||
| 16 | DLE | $P | 48 | 0 | 0 | 80 | P | P | 112 | p | +P | |||
| 17 | DC1 | $Q | 49 | 1 | 1 | 81 | Q | Q | 113 | q | +Q | |||
| 18 | DC2 | $R | 50 | 2 | 2 | 82 | R | R | 114 | r | +R | |||
| 19 | DC3 | $S | 51 | 3 | 3 | 83 | S | S | 115 | s | +S | |||
| 20 | DC4 | $T | 52 | 4 | 4 | 84 | T | T | 116 | t | +T | |||
| 21 | NAK | $U | 53 | 5 | 5 | 85 | U | U | 117 | u | +U | |||
| 22 | SYN | $V | 54 | 6 | 6 | 86 | V | V | 118 | v | +V | |||
| 23 | ETB | $W | 55 | 7 | 7 | 87 | W | W | 119 | w | +W | |||
| 24 | CAN | $X | 56 | 8 | 8 | 88 | X | X | 120 | x | +X | |||
| 25 | EM | $Y | 57 | 9 | 9 | 89 | Y | Y | 121 | y | +Y | |||
| 26 | SUB | $Z | 58 | : | /Z | 90 | Z | Z | 122 | z | +Z | |||
| 27 | ESC | %A | 59 | ; | %F | 91 | [ | %K | 123 | { | %P | |||
| 28 | FS | %B | 60 | < | %G | 92 | \ | %L | 124 | | | %Q | |||
| 29 | GS | %C | 61 | = | %H | 93 | ] | %M | 125 | } | %R | |||
| 30 | RS | %D | 62 | > | %I | 94 | ^ | %N | 126 | ~ | %S | |||
| 31 | US | %E | 63 | ? | %J | 95 | _ | %O | 127 | DEL | %T, %X, %Y, %Z | |||
[edit] External links
- Download Open-Source Barcode Generator for Code 39 (C++, VB, Excel Macros, etc.)
- EAN MediaWiki extension, a MediaWiki extension, visualizing Code39/Codabar/EAN-13/EAN-8/UPC-A barcodes by specifying only the numbers/symbols in the wiki-text.
- Search for free Code 39 fonts using Google
|
||||||||||||||||||||||||||


