Click here to Skip to main content
13,194,413 members (49,466 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 2 Nov 2005

Drawing UPCA/E barcodes

, 2 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Drawing UPCA/E barcodes based on Neil Van Eps' and Rainman_63's articles.


I had introduced how to draw an EAN13 barcode in my last article. This article is about UPC barcodes, with two instances: UPCA and UPCE.

UPCA barcode

UPCA and UPCE background

UPCA is the most commonly symbology (at least in the US), you can find them at supermarkets, bookstores… UPCA uses 11 digits (0 through 9) to encode its message data, the 12th character is the check digit. UPCA can be divided into 4 parts: system number, manufacturer code, product code, and check digit. We also follow this structure to draw the barcode.

The UPCE is a variation of UPCA. By removing some continued zeros, the UPCE compresses the message data, so it is about half the size of a UPCA barcode. UPCE is used on small packaging, where UPCA could not fit well.

UPCE also consists of 4 parts, but you can only see 3 parts, as in the picture below:

UPCE Barcode

“0” is the number system, “123456” is the manufacturer code and product code compressed and joined into one, and the last number is the check digit and it can be calculated from the original UPCA message data. “can be”, this means the compressed message is used to generate the check digit (just my opinion, should be wrong ?).

UPCE compresses the manufacturer code and product code only. In this article, I have not discussed how to convert message data from UPCA to UPCE yet, but I promise to work on that after my examination.

Encoding UPCA and UPCE

Actually, UPCA is a subset of EAN13 that the first number is set to zero. That is you can use the EAN13 encoding method to encode UPCA. UPCA uses two different patterns, the left pattern used to encode the number system and the manufacturer code, and the right pattern used to encode the product code and check digits. Please see the table below.

NumberLeft DigitsRight Digits

Table 1

‘s’ represents space and ‘b’ represents bar.

UPCE uses the Left Digits Odd Parity and the Right Digits Even Parity characters set from the EAN13 encoding standard. Please see the table below:

NumberLeft Digits
Odd ParityEven Parity

Table 2

Additionally, UPCE may only be used if the system is 0 or 1. The encoded characters are encoded with odd and even parity by the Left Digits Odd and the Left Digits Even from the table above. The parity used for each character depends on the number system and the check digit. See the table below.

Check DigitNumber System

Table 3


An example may be useful here. I will show you how to encode the UPCA message data: “01234567890”. The message consists of three parts (remember that the check digit is not available at this time):

  • 0 - System number
  • 12345 – Manufacturer code
  • 67890 – Product code

We calculate the check digit by scanning from the beginning to the end of the message. With each character, if its position in the string is odd, multiply it by 3. Otherwise, just add it into the sum.

0*3 + 1 + 2*3 + 3 + 4*3 + 5 + 6*3 + 7 + 8*3 + 9 + 0*3  =  85

In this case, the next number following 85 that is divisible by 10 is 90. We need to add 5 to 85 to get 90, and 5 is our check digit. Modulus of 10 is taken from 85, and this is subtracted by 10 again to get 5.

10 – (85%10) = 5

And now, our result is “0 12345 67890 5”.

The same method is used to calculate the check digit for the UPCE message data, but the length of the UPCE message data is shorter than that for UPCA. In fact, the check digit for UPCE must be calculated from the original UPCA message data (non-compressed message).

UPC Class

My UPC is based on the CBarcode class that has been introduced in Neil Van Eps' series of articles. It’s necessary to introduce this class again.

class CBarcode
        void LoadData(CString csMessage, double dNarrowBar, double dFinalHeight, 
                      HDC pDC, int nStartingXPixel, int nStartingYPixel, 
                      double dRatio = 1.0);
        virtual void DrawBitmap() = 0;
        virtual void BitmapToClipboard() = 0;
        virtual ~CBarcode();
        long GetBarcodePixelWidth();
        long GetBarcodePixelHeight();
        CString m_csMessage;
        HDC m_hDC;
        long m_nFinalBarcodePixelWidth;
        long m_nNarrowBarPixelWidth;
        long m_nPixelHeight;
        long m_nStartingXPixel;
        long m_nStartingYPixel;
        long m_nSymbology;
        long m_nWideBarPixelWidth;
        virtual void DrawPattern(CString csPattern) = 0;

And here is the CUPC class declaration. It's implemented to draw UPCA and UPCE barcodes.

class CUPC : public CBarcode  
    CUPC(int nSymbology);

    virtual ~CUPC();
    void LoadData(CString csMessage, double dNarrowBar, 
                  double dFinalHeight, long nGuardbarHeight, 
                  HDC hDC, int nStartingXPixel, 
                  int nStartingYPixel, double dRatio);
    void DrawBitmap();
    void BitmapToClipboard();

    long m_nGuardbarHeight;
    long CalculateCheckSumDigit();

    CString RetrieveSystemNumberPattern(int iSystemNumber/* = 0*/, int iNumber);

    //used for encoding UPCE barcode
    CString RetrieveLeftOddParityPattern(int iNumber);
    //used for encoding UPCE barcode
    CString RetrieveLeftEvenParityPattern(int iNumber);
    CString RetrieveLeftPattern(int iNumber);
    CString RetrieveRightPattern(int iNumber);

    void DrawPattern(CString csPattern);

    void DrawUPCA();
    void DrawUPCE();

Drawing a UPC Barcode

void CUPC::DrawUPCA()
    int i, tmpGuardBarHeight;
    DrawPattern("sssssssss"); // draw quite zone    
    DrawPattern("bsb"); // draw lead

    // draw number system
    tmpGuardBarHeight = m_nGuardbarHeight;
    m_nGuardbarHeight = 0;

    for (i = 1 ; i < 6 ; i++)// draw manufacturer code

    m_nGuardbarHeight = tmpGuardBarHeight;

    DrawPattern("sbsbs"); // draw separator bars

    tmpGuardBarHeight = m_nGuardbarHeight;
    m_nGuardbarHeight = 0;

    for (i = 6 ; i < 11 ; i ++) // draw product code

    m_nGuardbarHeight = tmpGuardBarHeight;
    //draw check sum digits
    DrawPattern("bsb"); // draw trailer bars
    DrawPattern("sssssssss"); // draw quite zone 


void CUPC::DrawUPCE()
    int i,nCheckDigit, nSystemNumber, tmpGuardBarHeight;

    nSystemNumber = (int)m_csMessage.GetAt(0)-48;
    nCheckDigit = CalculateCheckSumDigit();
    CString strSystemNumberPattern = 

    DrawPattern("sssssssss"); // draw quite zone    
    DrawPattern("bsb"); // draw lead
    tmpGuardBarHeight = m_nGuardbarHeight;
    m_nGuardbarHeight = 0;
    for (i = 1 ; i < 7 ; i ++){
        if (strSystemNumberPattern[i-1] == 'O')

        if (strSystemNumberPattern[i-1] == 'E')

    m_nGuardbarHeight = tmpGuardBarHeight;

    DrawPattern("sbsbs"); // draw separator bars
    DrawPattern("b"); // draw trailer bars
    DrawPattern("sssssssss"); // draw quite zone 


Using the code

Drawing UPCA

CUPC upc(UPCA); // create UPCA barcode
HDC hDC = m_staticBitmap.GetDC()->m_hDC;

Drawing UPCE

CUPC upc(UPCE); // create UPCE barcode
HDC hDC = m_staticBitmap.GetDC()->m_hDC;


I am an inexperienced student in coding. Although I started to study Barcode solutions since last year, this is the first time I am applying it. The barcode has been commonly used everywhere, but it is so fresh a topic in my country (Vietnam). I would like to thank Van Neil Eps and rainman_63 again. I would appreciate getting your feedback.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Lam Do
Software Developer (Senior)
Vietnam Vietnam
I joined CodeProject since working in MFC/C++ applications. For some personal reasons, I switched to Flash/Flex web applications and joined Pyramid Consulting Vietnam. I left Pyramid Consulting Vietnam in 2009 and started my free-lance jobs. Now I am a professional multimedia developer with specialized in Flash/Flex technologies and intend to turn back this community as a chance to contribute my knowledge in Flash/Flex technologies.

You may also be interested in...

Comments and Discussions

Questiongenerate upc-a using c# or vb Pin
jackmay23-Nov-14 21:54
memberjackmay23-Nov-14 21:54 upc barcode sdk Pin
Zacbr1-Feb-12 3:56
memberZacbr1-Feb-12 3:56 
GeneralI am overwhelmed. Pin
WREY2-Nov-05 9:13
memberWREY2-Nov-05 9:13 
First of all, I am no barcode expert, but just from reading this article I feel equiped enough to take on such an assignment. It is clear, informative, and well written.

For someone whose country does not have English as its mother tongue, you certainly handle it better than a lot of people whose country has it as their mother tongue.

Judging from your sample code, I believe you are modest to think of yourself as an "inexperienced student in coding".

I have not tested your work, but from the way you explained matters, I believe you are a clean and careful programmer, and I'm going to stick my neck out and give your article a '5'.

Good Work!

Big Grin | :-D


Fortes in fide et opere!

-- modified at 11:22 Thursday 3rd November, 2005
GeneralRe: I am overwhelmed. Pin
lam do thanh3-Nov-05 17:57
memberlam do thanh3-Nov-05 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171018.2 | Last Updated 2 Nov 2005
Article Copyright 2005 by Lam Do
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid