Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / Visual Basic
Article

USPS OneCode Barcodes

Rate me:
Please Sign up or sign in to vote.
4.83/5 (20 votes)
3 Oct 2008Apache2 min read 158.4K   3.6K   54   46
USPS One Code - Intelligent Mail Barcode (4-State Customer Barcode) - implementation with VB 2008

Introduction

This is not a typical article. The focus is on Intelligent Mail Barcode (4 State Customer Barcode) or USPS OneCode implementation as a direct solution for anyone who needs it. The downloads include one VB.NET and one C# class – VS2008 with .NET 3.5 syntax - that generate the string expression of OneCode barcodes and decode a barcode string. The code (uncommented) is a modified fraction of the Pathenon project (covers all known 1D and 2D barcodes) that you will read here on The Code Project in the near future, hopefully. I've noticed that there is no related article, so there you are with an ASAP solution for this new USPS symbology.

Basic Information

Intelligent Mail Barcode, also known as the USPS OneCode Solution or USPS 4-State Customer Barcode (abbreviated 4CB, 4-CB, or USPS4CB), is the latest barcode symbology of the United States Postal Services. It combines routing ZIP code information and tracking information into a single 4-state code. It effectively encodes data from POSTNET and PLANET barcodes into a single barcode while providing a greater range of tracking data.

A 4-state barcode is based on a tracker with ascenders and descenders. The four possible states are tracker (neither ascender nor descender), full (both ascender and descender), ascender only, and descender only. The Intelligent Mail barcode consists of 65 bars as a result of encoding data fields of the following types:

Tracking Code

  • Barcode Identifier as 2 digits (2nd digit must be 0–4)
  • Service Type Identifier as 3 digits
  • Mailer Identifier as 6 or 9 digits and
  • Serial Number as 9 (when used with 6 digit Mailer ID) or 6 (when used with 9 digit Mailer ID) digits

Routing Code

  • Delivery Point ZIP Code as 0, 5, 9, or 11 digits

The above fields, with a maximum of 31 digits in length, are encoded via a complex process (involving big integers and transposed lookups) to bars expressed as a string with 65 characters in the range of A, D, T, F. More information and specifications can be found here.

Conclusion

Note once again that this is not a typical article, but a direct solution for people dealing with this complex barcode symbology.

Thanks for your time.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0



Comments and Discussions

 
GeneralRe: Incorrect Decode Example Pin
Gylfi Ingvason27-May-09 8:59
Gylfi Ingvason27-May-09 8:59 
GeneralRe: Incorrect Decode Example Pin
bheiland27-May-09 13:59
bheiland27-May-09 13:59 
AnswerRe: Incorrect Decode Example Pin
bheiland27-May-09 14:04
bheiland27-May-09 14:04 
GeneralRe: Incorrect Decode Example Pin
bheiland27-May-09 14:23
bheiland27-May-09 14:23 
GeneralRe: Incorrect Decode Example Pin
Gylfi Ingvason28-May-09 7:02
Gylfi Ingvason28-May-09 7:02 
GeneralRe: Incorrect Decode Example Pin
bheiland28-May-09 11:28
bheiland28-May-09 11:28 
GeneralRe: Incorrect Decode Example Pin
Gylfi Ingvason29-May-09 5:20
Gylfi Ingvason29-May-09 5:20 
GeneralRe: Incorrect Decode Example [modified] Pin
fwsouthern1-Nov-10 17:31
fwsouthern1-Nov-10 17:31 
These changes are not reflected in either your VB or your C# code -- can you please make the changes & repost your source code? Also, please run the 4 samples from the USPS IMb Specification SPUSPSG - I'm experiencing progressive error as the DP ZIP content increases:
"01234567094987654321"
"0123456709498765432101234"
"01234567094987654321012345678"
"0123456709498765432101234567891"

This fixes the error:
For i = 0 To 9
Dim test = ad(i), index = Array.IndexOf(table5of13, test)
If (index < 0) Then
test = Not test And 8191
index = Array.IndexOf(table5of13, test)
If (index < 0) Then
test = ad(i) 'bgh
index = Array.IndexOf(table2of13, test)
If (index < 0) Then ' bgh
test = Not test And 8191 'bgh
index = Array.IndexOf(table2of13, test) 'bgh
End If 'bgh
index += 1287
End If
End If
ad(i) = index
Next
ad(9) = CType(ad(9) \ 2, Integer)
If (ad(0) > 658) Then ad(0) -= 659

OneCodeMathMultiply(byteArray, 13, 659) 'added missing multiply

OneCodeMathAdd(byteArray, 13, ad(0))
For i = 1 To 8
OneCodeMathMultiply(byteArray, 13, 1365)
OneCodeMathAdd(byteArray, 13, ad(i))
Next
OneCodeMathMultiply(byteArray, 13, 636)
OneCodeMathAdd(byteArray, 13, ad(9))
r = OneCodeMathMod(byteArray, 10)
result = r.ToString() & result
For i = 2 To 18 'bgh only loop 2 to 18 because the 19th test should be mod 5
OneCodeMathAdd(byteArray, 13, -r)
OneCodeMathDivide(byteArray, 10)
r = OneCodeMathMod(byteArray, 10)
result = r.ToString() & result
Next

OneCodeMathAdd(byteArray, 13, -r)'bgh
OneCodeMathDivide(byteArray, 10) 'bgh
r = OneCodeMathMod(byteArray, 5) 'bgh 19 divide by 10 is then mod 5
result = r.ToString() & result 'bgh
OneCodeMathAdd(byteArray, 13, -r) 'bgh
OneCodeMathDivide(byteArray, 5) ' bgh now divide by 5 and mod 10
r = OneCodeMathMod(byteArray, 10) 'bgh
result = r.ToString() & result ' bgh last result
OneCodeMathAdd(byteArray, 13, -r)' bgh
OneCodeMathDivide(byteArray, 10) 'one last divide by 10
' at this point all that remains is the encoded DP ZIP - convert to int and skip to the "if rest > 1000100001" section

I also have a JavaScript/JScript version validated against all USPS test cases.

modified on Thursday, November 4, 2010 1:12 AM

GeneralRe: Incorrect Decode Example Pin
bheiland14-Apr-11 3:31
bheiland14-Apr-11 3:31 
GeneralRe: Incorrect Decode Example [modified] Pin
fwsouthern14-Apr-11 3:46
fwsouthern14-Apr-11 3:46 
GeneralThank you for sharing your work... Pin
accusteve4-Nov-08 8:08
accusteve4-Nov-08 8:08 
GeneralFailed Test Cases Pin
Member 452829222-Sep-08 4:56
Member 452829222-Sep-08 4:56 
AnswerRe: Failed Test Cases Pin
the retired24-Sep-08 1:39
the retired24-Sep-08 1:39 
QuestionPHP Version? :D Pin
voxdisc19-Aug-08 21:30
voxdisc19-Aug-08 21:30 
GeneralGREAT! Pin
hpellegrino15-Aug-08 10:07
hpellegrino15-Aug-08 10:07 
GeneralCooool! Pin
tharpa31-Oct-07 7:48
tharpa31-Oct-07 7:48 
QuestionOneCode string Decoder? Pin
res416-Oct-07 6:16
res416-Oct-07 6:16 
AnswerRe: OneCode string Decoder? Pin
the retired16-Oct-07 22:53
the retired16-Oct-07 22:53 
GeneralRe: OneCode string Decoder? Pin
tharpa14-Mar-08 8:00
tharpa14-Mar-08 8:00 
NewsOneCode Decoder is finally here PinPopular
the retired16-Aug-08 14:36
the retired16-Aug-08 14:36 
GeneralRe: OneCode Decoder is finally here Pin
bheiland29-May-09 2:45
bheiland29-May-09 2:45 
GeneralRe: OneCode Decoder is finally here Pin
crobbins6-Jan-11 8:53
crobbins6-Jan-11 8:53 

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.