5,276,801 members and growing! (15,679 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General     Intermediate

Calculating UPS Tracking Number Check Digit

By stebo0728

Algorithm for calculating final check digit for UPS tracking number
C#, Windows, .NET, Visual Studio, Dev

Posted: 7 Nov 2007
Updated: 7 Nov 2007
Views: 6,940
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
1 vote for this article.
Popularity: 0.00 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 100.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This article serves to describe the algorithm used to calculate a UPS Tracking Number. It took me a while to figure this out, researching mostly. I thought I would make it a bit easier of others by describing it here.

Using the code

The following code is a generic method that can be used to calculate a check digit for a UPS Tracking Number. The input for the method is a String, but you could rework it to use a raw Char[] if wish. This method leaves the rest of the tracking number up to you. it takes a 15 character sequence, in this case a String, and calculates the check digit using this sequence. For information sake, I will describe how the company I work for generates tracking numbers. This is up to you, as only two portions are required by UPS, the rest you can make up on your own.

  1. The first two characters must be "1Z".
  2. The next 6 characters we fill with our UPS accout number "XXXXXX"
  3. The next 2 characters denote the service type:
    * "01" for Next Day Air shipments
    * "02" for Second Day Air shipments
    * "03" for Groud shipments
  4. The next 5 characters is our invoice number (Our invoices are 6 digits, we drop the first digit e.g 123456 invoice would yield 23456 characters.
  5. The next 2 digits is the package number, zero filled e.g. Package 1 is "01", 2 is "02"
  6. The last and final character is the check digit.

First of all you will notice that the described sequence above gives is 17 characters, where as we only need 15 to calculate the check digit. To do this, we drop the "1Z" portion, and only use the last 15 characters in the method.

Next let me take a moment to outline the algorithm used to generate the check digit, then you'll see the method below the outline:

  1. Start a running total
  2. Examine each character in the sequence
    • If the character is in an odd position (e.g 1st,3rd,5th....) then
      • If the character is numeric then add the numeric value to the running total
      • If the character is alpha, then
        • Calculate n to be (ASCII value of character - 48)
        • Calculate x to be ((2 * n) - (9 * INT(n/5))) where INT(n/5) returns n/5 rounded down to the next integer (e.g 34.3 would be 34 but also 34.8 would be 34)
        • Add x to the running total
    • If the character is in an even position (e.g 2nd, 4th, 6th......) then
      • If the character is numeric then
        • Calculate n to be (2 * numeric value of character)
        • Add n to the running total
      • If the character is alpha then
        • Calculate n to be (ASCII value of character - 48)
        • Add n to the running total
  3. Calculate x to be (Running total modulo 10)
    • If x = 0 then x is the check digit
    • If x > 0 then
      • Calculate y to be (10 - x)
      • y is the check digit

Here is the method:

//

// UPS Check Digit Calculation Method

//


private int CalculateCheckDigit(String trk)
{
   int checkdigit = 0;
   char[] chars = trk.ToCharArray();
   int charindex = 1;
   int runningtotal = 0;
   foreach(Char ch in chars)
   {
      if((charindex % 2) == 0) //Indicates character in even position

      {
         int testeven;
         if(Int32.TryParse(ch.ToString(), out testeven) == true) // Indicates numeric value

         {
            runningtotal += (2 * testeven);
         }
         else // Indicates alpha value

         {
            int asciivalue = System.Convert.ToInt32(ch);
            int n = asciivalue - 48;
            runningtotal += n;
         }
      }
      else // Indicates character in odd position

      {
         int testodd;
         if(Int32.TryParse(ch.ToString(), out testodd) == true) // Indicates numeric value

         {
            runningtotal += testodd;
         }
         else // Indicates alpha value

         {
            int asciivalue = System.Convert.ToInt32(ch);
            int n = asciivalue - 48;
            int x = ((2 * n) - (9 * (int)(n / 5)));
            runningtotal += x;
         }
      }
      charindex++;
   }
   int x = (runningtotal % 10);
   if(x == 0) checkdigit = x;
   else if(x > 0) checkdigit = (10 - x);
   return checkdigit;
}

License

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

stebo0728



Location: United States United States

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralUpdates to original functionmemberheyheyitsdavid7:01 20 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Nov 2007
Editor:
Copyright 2007 by stebo0728
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project