The UPC Bar Code - Reading and checking UPC barcodes
Handed out: 11/29/05
Due: by 10:00pm on 12/05/03
Here are the rules for UPC barcodes:
- Barcodes consist of black and white vertical lines of 4 thicknesses.
- The four thicknesses, from thinnest to thickest are 1, 2, 3, and 4.
- The barcode begins and ends with 3 thinnest vertical bars(black,white,black).
Let's call this sequence "the margin" (technically, they are called
respectively "left guard bar" and "right guard bar")
- In the middle of the barcode are 5 thinnest lines, in sequence, white, black,
white, black, white. Let's call this sequence "the middle" (technically
it is called "central guard bar").
- Between the beginning margin and the middle is the product manufacturer id.
Between the middle and the end margin is the item id.
- The lines in the manufacturer id (and the same for the item id) are grouped
to represent digits. There are in total 12 digits, 6 in the manufacturer id,
6 in the item id. The codes used to represent the digits in the manufacturer
id are
Digit Thickness Colors
0 3211 WWWBBWB
1 2221 WWBBWWB
2 2122 WWBWWBB
3 1411 WBBBBWB
4 1132 BWBBBWW
5 1231 WBBWWWB
6 1114 WBWBBBB
7 1312 WBBBWBB
8 1213 WBBWBBB
9 3112 WWWBWBB
You will have noticed that all digits have 4 lines and occupy the same
amount of space: if you sum the thicknesses, you always get 7.
- The codes used to represent the digits in the item id are the same,
but now the colors are reversed. So '0' becomes "BBBWWBW", '1' becomes
"BBWWBBW", etcetera.
- The last digit of the item id is not really part of the item it, it is a
check digit used to determine if there was a read error when scanning the
barcode. Thus in the barcode we have: the start margin (BWB), the manufacturer
id (6 digits), the middle (WBWBW), the item id (5 digits), the check digit,
and the end margin (BWB). The check digit is computed as follows:
Step 1: Add the digits at odd positions together (the first digit is at position 1)
Step 2: Multiply this sum by 3
Step 3: Add the digits at even positions together
Step 4: Add results of steps 2 and 3.
Step 5: Subtract this sum from the next multiple of 10: this is the check digit
For example, if we are given the barcode 036000291452 the computation becomes:
sum1 = (0+6+0+2+1+5)*3 = 42; sum2 = 3+0+0+9+4 = 16. sum1+sum2 = 58
Thus the check digit is 60-58 = 2 as in fact is the case (the 12th digit of the
barcode is 2)
Additional information about UPC barcodes can be found at
http://en.wikipedia.org/wiki/Universal_Product_Code
and at
http://electronics.howstuffworks.com/upc1.htm
Your program should in a loop prompt the user to enter a barcode in decimal representation (something like 036000291452 above). The program should in correspondence indicate if the check digit is correct. If not it should print out the correct check digit. Then if correct it should convert the decimal representation to a color representation - in the case of 036000291452 the color representation is
BWBWWWBBWBWBBBBWBWBWBBBBWWWBBWBWWWBBWBWWWBBWB
WBWBWBBWBBWWBBBWBWWBBWWBBWWBWWWBBBWWBBBWBBWBBWWBWB
print that color representation, then convert it back to a decimal representation. Of course you should get back the original decimal representation.
Email your program to the Teaching Assistant.