Introduction:Although we don't give them much special attention, UPC bar codes are around us all the time. You will find that just about every package you see has a UPC bar code printed on it. In fact, every item that you purchase from department stores, grocery stores, etc. has a UPC bar code on it somewhere. But have you ever wondered where these codes come from and what they mean? In this mini-project, we will answer this question by explaining how the string 'CSE370' could be represented in a UPC bar code.
What's in a bar code?Bar codes are machine-readable symbols made of patterns of black and white bars, or in some cases checkerboard-like grids. There are different styles of bar codes called symbologies. UPC is one of many examples of different symbologies.
UPC stands for Universal Product Code. Formally adopted by the grocery industry on April 3, 1973 as the standard bar code symbology for product marking, the UPC was one of the first bar code symbologies to gain wide acceptance.
How do UPC bar codes work?UPC bar codes are a machine-scannable encoding of 10-digit decimal numbers preceded by a 1-digit decimal number "number system character" and followed by a 1-digit decimal number checksum. The "number system character" specifies the type of bar code as one of the following:
The checksum digit is used to help verify that the code was properly scanned. Taking the first 11 digits of the code (the 1-digit "number system character" followed by the 10-digit item code), the checksum is computed with the following algorithm:
The bar code itself is composed of alternating black and white bars of varying size. The bars used to create the bar code come in four relative sizes, the largest being four times as wide as the smallest. Each digit in the 12-digit number is encoded with a unique four-bar sequence of total width 7:
The UPC bar code begins at the left with a "1 - 1 - 1" start code (black - white - black). Following the start code are the first 6 digits of the code, each composed of four bars (white - black - white - black) as defined by the table above. In the middle of the code is a "1 - 1 - 1 - 1 - 1" midpoint code (white - black - white - black - white). Then come the remaining 6 digits of the code, each also composed of four bars (this time black - white - black - white). The end of the bar code is then signalled with a "1 - 1 - 1" stop code (black - white - black). A well-formed UPC bar code might look something like:
Encoding 'CSE370' in a UPC bar code:The largest 10-digit number our bar code can store is 9,999,999,99910. That number requires 34-bits to store in binary form (10010101000000101111100011111111112). Therefore, any number that fits into 33 bits or less can be encoded with a UPC bar code.
To encode our class description string 'CSE370', we'll use an encoding scheme intended to be general enough to describe any class offered at the University of Washington. We'll encode our string in three sections::
Finally, we line up all our bits into one big 33-bit binary integer and convert it to decimal. If the resulting decimal number is of length < 10, we right-align our number in 10 digits and pad to the left with zeros (i.e. '12345' >> '0000012345'). That number is then encoded into bar code.
Following this encoding scheme, our string 'CSE370' would be encoded into a 10-digit decimal number like this:
Since we're using a custom encoding that no one else is going to know how to interpret, our "number system character" has to be a 4. Thus our 11-digit code is:
Next, we have to compute our checksum digit. Following the algorithm outlined above:
Thus, our checksum digits is 5, and our final 12-digit code is:
Now, following the table listed above, we encode our twelve digit number into bar codes:
And finally, after putting together all the bars, we have: