## IUG FORUM

Stay always connected!

# Resolved Check Digit calculation

0
Undo
1. Andy Helck
2. Sierra/ Millennium/ Encore
3. Friday, 14 February 2020
Hi IUG members! I have a coding project and I am looking for the algorithm to calculate the checksum digit for a record number. For example a bib number in our system is b6059510 which is 7 digits of information. The checkdigit forms the 8th digit and should be '3'. I am trying to find the algorithm to calculate this.

BTW, I know as a practical matter I can often just append the letter 'a' to my 7 digits but I am looking for the precise digit that Sierra/Milllenium would calculate.

Thanks!

ahelck@telluridelibrary.org

derek.brown@rhpl.org
Comment
Responses (1)
0
Undo
I did some research and here is what I found. I will list 2 examples, the first is in javascript, the second in C#.

Here is the JS example courtesy of Jim Nicholls <jim.nicholls@sydney.edu.au> There is a complete project you can download here:
https://github.com/SydneyUniLibrary/sierra-record-check-digit

function calcCheckDigit(recordNumber)
{
let m = 2
let x = 0
let i = Number(recordNumber)
while (i > 0)
{
let a = i % 10
i = Math.floor(i / 10)
x += a * m
m += 1
}
const r = x % 11
return r === 10 ? 'x' : String(r)
}

and here is my translation into C#. Note that the record number is represented as a string, so I peel off the digits with substring rather than perform modulo arithmetic as in the example above. I've only done a little testing but it seems to be OK.

public static string AddCheckDigit(this string s, int nDataDigits) // for us its 7 data digits + the checkdigit for 8
{
{
char[] charsPlusOne = (s + " " .ToCharArray(); // extra space at the end is for the check digit

int x = 0;
int m = 2;
for (int i = 0; i < nDataDigits; i++)
{
int a = charsPlusOne[(nDataDigits - 1) - i] - '0'; // 6,5,4,3,2,1,0
x += a * m;
m++;
}
int r = x % 11;
char cd = (r == 10) ? 'x' : (char)(r + '0');
charsPlusOne = cd;

return new string(charsPlusOne);
}
else return s + "a";
}

ahelck@telluridelibrary.org
Comment