USPS Standards for Check Digit Computation in vb.net

I have been working on this for a while.

Basically we need to implement a check digit sum number for this address correction service.  When a alpha+number is given, it should convert to this check digit algorithm  Eg if I input ID1234, the check digit sum would be : ID12343.

So here we go. I will try to get the specifications uploaded to the file section of my blog if the proxy will allow me to. It is Extract-from-pub8a.doc if it appears.

http://cid-a0d71e1614e8dbf8.skydrive.live.com/embedrowdetail.aspx/Public/Extract-from-pub8a.doc

Imports System.Text
 
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim vArrayTest() As String = {"ABC999MM/321", "ANIT0281", _
            "0012////8", "811NIS0001119", "12345678907", _
            "1234569", "ID2359", "QWERTY1", "218XN91LMS4", "TMS1112/623"}
 
        For Each strTest As String In vArrayTest
            If CheckSumString(strTest.Substring(0, strTest.Length - 1)) = strTest Then
                Debug.WriteLine(strTest & " PASS")
            Else
                Debug.WriteLine(strTest & "FAIL " & CheckSumString(strTest.Substring(0, strTest.Length - 1)))
            End If
        Next
 
    End Sub
 
    Private Function CheckSumString(ByVal strText As String) As String
 
        Dim b() As Byte
        Dim strDigits As String = String.Empty
        Dim intTotal As Integer
 
        strText = strText.Replace(" ", "")
        strText = strText.ToUpper()
        b = Encoding.UTF8.GetBytes(strText)
        For i As Integer = 0 To b.Length - 1
            b(i) = CByte(b(i) And &HF)
        Next
 
        For i As Integer = 0 To b.Length - 1 Step 2
            b(i) = CByte(b(i) * 2)
        Next
 
        For i As Integer = 0 To b.Length - 1
            If b(i) = 28 And i Mod 2 = 0 Then
                'Debug.Print strText, i, b(i)
                strDigits = strDigits & "10"
                'Debug.Print
            Else
                strDigits = strDigits & b(i)
            End If
        Next
 
        'Debug.Print "Digits: " & strDigits
 
        For i As Integer = 0 To strDigits.Length - 1
            intTotal = intTotal + CInt(strDigits.Substring(i, 1))
        Next
 
        Dim strTotal As String = intTotal.ToString()
        CheckSumString = strText & CStr(10 - CInt(strTotal.Substring(strTotal.Length - 1, 1))) mod 10
 
    End Function
 
    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
 
        ' Asc("/") = 47 and Asc("Z") = 90
        For i As Integer = 47 To 90
            Debug.Print(Chr(i), i And &HF)
        Next
 
    End Sub
 
End Class
About these ads
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: