|
-
Nov 6th, 2009, 10:01 AM
#1
Thread Starter
Fanatic Member
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)
-
Nov 6th, 2009, 10:07 AM
#2
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)
-
Nov 6th, 2009, 10:12 AM
#3
Thread Starter
Fanatic Member
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?
-
Nov 6th, 2009, 10:14 AM
#4
Re: check digit weights
Doesn't my code above do that?
-
Nov 6th, 2009, 10:20 AM
#5
Thread Starter
Fanatic Member
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.
-
Nov 6th, 2009, 10:26 AM
#6
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)
-
Nov 6th, 2009, 10:42 AM
#7
Thread Starter
Fanatic Member
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...
Last edited by bezaman; Nov 6th, 2009 at 10:52 AM.
-
Nov 6th, 2009, 10:57 AM
#8
Re: check digit weights
I get 8 with that sample data.
-
Nov 6th, 2009, 10:58 AM
#9
Thread Starter
Fanatic Member
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.
-
Nov 6th, 2009, 11:32 AM
#10
Thread Starter
Fanatic Member
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?
-
Nov 6th, 2009, 11:40 AM
#11
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?
-
Nov 6th, 2009, 12:36 PM
#12
Thread Starter
Fanatic Member
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.
-
Nov 25th, 2009, 03:12 PM
#13
Thread Starter
Fanatic Member
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|