Check Digit Help pls

johncassell

Well-known member
Joined
Jun 10, 2007
Messages
120
Location
Redcar, England
Programming Experience
Beginner
Hi There,

I need help calculating a checkdigit for a particular tank number...

A tank number is always 4 letters followed by 6 numbers and then 1 check digit. Example of tank number ABCD123456-3

Letters are given the value of A=1, B=2....Z=26

You convert ABCD123456 into all numbers (So, 1234123456)
You then multiply each one by the following values..
1 2 4 8 16 32 64 128 256 512
-----------------------------------------
Giving this list of numbers...
1 4 12 32 16 64 192 512 1280 3072
Add all resultant nos to give total = 5185
Divide total by 11 5185/11=471.3636364
Multiply whole no by 11 471 x11=5182
Subtract result from 1st total 5185-5182=3
The result is the check digit =3

Hope your still with me at this point and are able to give any pointers in how to calculate this in VB.

Thanks

John
 
Divide total by 11 5185/11=471.3636364
Multiply whole no by 11 471 x11=5182
Subtract result from 1st total 5185-5182=3

This is a pretty simple equation although you complicated it a little. What you are looking for is called a modulo, or the remainder of a division. In most programming languages, you will use the '%' character to use it just like any other opertor.

In VB.NET it works like this :

VB.NET:
MessageBox.Show((5185 Mod 11).ToString())

BTW, 471 x 11 = 5181, not 5182, so 5185 % 11 = 4, not 3
 
For every problem there are a 100 solutions. Here is mine.
VB.NET:
Public Class Form1
    Dim str As String = "ABCD123456-3"
    Dim ary(9) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tot As Integer = 0
        Dim mult As Integer = 1

        For x As Int16 = 0 To 9
            If x < 4 Then
                ary(x) = Asc(str.Substring(x, 1)) - 64
            Else
                ary(x) = Integer.Parse(str.Substring(x, 1))
            End If
        Next
        For x As Int16 = 0 To 9
            tot += ary(x) * mult
            mult *= 2
        Next
        tot = tot - (Int(tot / 11) * 11)

        MessageBox.Show(tot.ToString)

    End Sub
End Class
 
Last edited:
Back
Top