-
check digit weights
i'm trying to calculate the check digit for a scan line. my problem is it needs to be weighted 2121, right to left. so what i have to do is take each character in the string and convert it to it's ASCII equivalent then multiply that by either a 2 or 1 depending on the position of the character. how can i alternate the 2 to a 1 after each character?
Code:
Dim Product As Integer
Dim SumOfProducts As Integer
For Each row As DataRow In Table.Rows
For Each ch As Char In row("PRESCAN").ToString
Product = Asc(ch) * 2
SumOfProducts = SumOfProducts + Product
Next
row("CHKDGT") = SumOfProducts Mod 10
Next
DataAdapter.Update(Table)
-
Re: check digit weights
Not sure exactly what you mean, but are you looking for something like this:
Code:
Dim Product As Integer
Dim SumOfProducts As Integer
Dim Factor As Integer = 1
For Each row As DataRow In Table.Rows
For Each ch As Char In row("PRESCAN").ToString
Factor = If(Factor = 2, 1, 2)
Product = Asc(ch) * Factor
SumOfProducts = SumOfProducts + Product
Next
row("CHKDGT") = SumOfProducts Mod 10
Next
DataAdapter.Update(Table)
-
Re: check digit weights
let me explain it this way. here is my scan line sample:
3273
i have to convert each character to ASCII
3=51
2=50
7=55
3=51
then i have to multiply each ASCII value by the correct weight
51*1
50*2
55*1
51*2
does this make more sense?
-
Re: check digit weights
Doesn't my code above do that?
-
Re: check digit weights
so you set factor = 1
then you check to see if factor does not equal one
and then you set the factor to 2
right? am i reading this correctly.
-
Re: check digit weights
But if it is 2 it will be set back to 1 via that IF() function.
Although you may need to reinitialize the factor back to 1 before you loop the set of chars each time to ensure you are starting with a factor of 2.
Code:
Dim Product As Integer
Dim SumOfProducts As Integer
Dim Factor As Integer = 1
For Each row As DataRow In Table.Rows
Factor = 1
For Each ch As Char In row("PRESCAN").ToString
Factor = If(Factor = 2, 1, 2)
Product = Asc(ch) * Factor
SumOfProducts = SumOfProducts + Product
Next
row("CHKDGT") = SumOfProducts Mod 10
Next
DataAdapter.Update(Table)
-
Re: check digit weights
i'm still not getting the correct results. the check digit for this scan line:
000032734000012500175002000000000000H150001CUR00000
should be 7
i'll keep trying... maybe my calculations are incorrect...
-
Re: check digit weights
I get 8 with that sample data.
-
Re: check digit weights
so do i. i must have the formula incorrect. i thought that is how i was supposed to calculate the check digit. maybe i'm wrong.
-
Re: check digit weights
I know i'm just fishing here, but does anybody know how to figure out a check digit formula just by looking at the scanline and check digit?
-
Re: check digit weights
There seem to be multiple ways to do the calculation based on some different standards.
http://en.wikipedia.org/wiki/Check_digit
Do you know the exact formula you are supposed to be using?
-
Re: check digit weights
No. that's my problem. they gave me a scan line with a check digit and that's it. i guess i'm just supposed to figure it out.
-
Re: check digit weights
I got the formula and it is different than what i anticipated. The weights are now 7,3,1.
51*7
50*3
55*1
so i'm wondering who to modify the above code to do this?