Results 1 to 13 of 13

Thread: check digit weights

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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)

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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?

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: check digit weights

    Doesn't my code above do that?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: check digit weights

    I get 8 with that sample data.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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?

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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
  •  



Click Here to Expand Forum to Full Width