Results 1 to 7 of 7

Thread: Doing Calculations on Integers in an array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Doing Calculations on Integers in an array

    I am working on verifying Vin numbers and cannot figure out how to take my converted array, which is the result of the first sub and multiple each one of the numbers to set of weighted values.

    For instance I have taken the VIN shown in the string below and converted it to this 41312348565041183, which is correct. Now I need to take each number in that string and multiply it like this:

    1st Position * 8
    2nd position * 7
    3rd position * 6
    4th position * 5

    Would be

    4 * 8 = 32
    1 * 7 = 7
    3 * 6 = 18
    1 * 5 = 5

    and so on. Here is the code I have so far. My trouble is in the CalcVin Sub.


    VB Code:
    1. Private Sub ConvertVin()
    2.  
    3.         Dim strVin As String = "4A3AK34Y5WE041183"
    4.         Dim arrVin(16) As Char
    5.         arrVin = strVin.ToCharArray
    6.  
    7.         Dim c As Char
    8.         For Each c In arrVin
    9.             txtResults.Text += CStr(VinAlphaConvert(c))
    10.         Next c
    11.  
    12.     End Sub
    13.  
    14.     Private Sub CalcVin()
    15.         Dim strConVin As String = txtResults.Text
    16.         Dim arrVin(16) As Integer
    17.  
    18.         arrVin(0) += arrVin(0) * 8
    19.         MessageBox.Show(CStr(arrVin(0)))
    20.  
    21.         'Dim c As Char
    22.         'arrVin(0) += CChar(CStr(CInt(Val(c)) * 8))
    23.  
    24.         ' I need to take each of the 17 integers and
    25.         ' multiply them by a weight factor like this
    26.         '1st = 8     10th = 9
    27.         '2nd = 7     11th = 8
    28.         '3rd = 6     12th = 7
    29.         '4th = 5     13th = 6
    30.         '5th = 4     14th = 5
    31.         '6th = 3     15th = 4
    32.         '7th = 2     16th = 3
    33.         '8th = 10    17th = 2
    34.         '9th = check digit

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Doing Calculations on Integers in an array

    You could just loop through, having a select case on the value 0 - 16, then changing each case like that...
    VB Code:
    1. For I As Integer = 0 To 16 Step 1
    2.        Select Case I
    3.            Case 0, 10
    4.                arrVin(I) = arrVin(I) * 8
    5.                'etc
    6.        End Select
    7. Next I
    Last edited by gigemboy; Apr 21st, 2006 at 06:02 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Doing Calculations on Integers in an array

    Thanks gigemboy. I am still having a lot of problems with this so I going to take one step back and make sure I am explaining this correctly. So before I get to that part I need to get the string of numbers from the textbox to become numbers and not text characters.

    Once I get them into numbers and then weight each number according to its position in the array I need to add them together to get a total sum. So to begin how do I get each number from the array so they are treated as numbers and not chars?

    Here is some pseudocode to help describe my task.

    Characters in the array = 1 2 3 (from a textbox)

    Character in the position 1 gets converted to an number and multiplied by 5
    Character in the position 2 gets converted to an number and multiplied by 10
    Character in the position 3 gets converted to an number and multiplied by 15

    1 * 5 = 5
    2 * 10 = 20
    3 * 15 = 45

    Add all weighted numbers together 5+20+45 = 70

    Am I even on the right track or is there a better way to do this?
    Last edited by FastEddie; Apr 21st, 2006 at 09:07 AM.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Doing Calculations on Integers in an array

    Well in your string above ("4A3AK34Y5WE041183"), the second character position is a letter, not a number (as well as several other letters in the string). So what values are used for those?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Doing Calculations on Integers in an array

    The first function turns any letters into numbers and leaves the numbers as they are. Here is the sub and the function:
    VB Code:
    1. Private Sub ConvertVin()
    2.  
    3.         Dim strVin As String = "4A3AK34Y5WE041183"
    4.         Dim arrVin(16) As Char
    5.         arrVin = strVin.ToCharArray
    6.  
    7.         Dim c As Char
    8.         For Each c In arrVin
    9.             txtResults.Text += CStr(VinAlphaConvert(c))
    10.         Next c
    11.  
    12.     End Sub
    13.  
    14.     Public Function VinAlphaConvert(ByVal str As String) As Integer
    15.         Dim int As Integer
    16.         Select Case str
    17.             Case "A" : int = 1
    18.             Case "B" : int = 2
    19.             Case "C" : int = 3
    20.             Case "D" : int = 4
    21.             Case "E" : int = 5
    22.             Case "F" : int = 6
    23.             Case "G" : int = 7
    24.             Case "H" : int = 8
    25.             Case "J" : int = 1
    26.             Case "K" : int = 2
    27.             Case "M" : int = 3
    28.             Case "N" : int = 4
    29.             Case "P" : int = 7
    30.             Case "R" : int = 9
    31.             Case "S" : int = 2
    32.             Case "T" : int = 3
    33.             Case "U" : int = 4
    34.             Case "V" : int = 5
    35.             Case "W" : int = 6
    36.             Case "X" : int = 7
    37.             Case "Y" : int = 8
    38.             Case "Z" : int = 9
    39.             Case Else
    40.                 int = CInt(str)
    41.         End Select
    42.         Return int
    43.     End Function
    That works find and turns 4A3AK34Y5WE041183 into 41312348565041183. Now I just to take each one of those numbers indivudualy and multiply them in the way I mentioned above.

    Maybe this will help if you look at this page VIN NUMBER CONTENT REQUIREMENT. About half way down the page there are 4 steps that explain exactly what I am trying to do.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Doing Calculations on Integers in an array

    You might want to just keep the return value of the alphaconvert in a string instead of an integer (or since you have that like it is, just use the .ToString method on the integer return value in order to loop through all characters so you dont have to change the function), that way you can just loop through each character of the number string, replacing as necessary, like below...
    VB Code:
    1. Dim MyString As String = "41312348565041183"
    2.         Dim ReturnString As String 'string to hold conversion
    3.         For Each Number As Char In MyString
    4.             Select Case Number
    5.                 Case Is = "1"c
    6.                     'append new value onto the string
    7.                     ReturnString &= (1 * 8).ToString
    8.                     'etc
    9.             End Select
    10.         Next
    11.         MessageBox.Show(ReturnString)
    12.         'now just convert ReturnString to an integer
    Last edited by gigemboy; Apr 21st, 2006 at 11:46 AM.

  7. #7
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Cool Re: Doing Calculations on Integers in an array

    There might be some errors with that website,...

    They failed to mention that the first digit of 9 means Brazil.

    Also, the 2nd letter V is also for Volkswagen, as explained here.

    I have owned numerous Volkswagens (hence my handle), and i know for a fact that my VINs have been:

    3VW... = Mexico
    9VW... = Brazil
    WVW... = Germany

    VIN's were standardized at seventeen characters in 1981. So any car made before that might not decode properly.

    The 9th character check digit explained.
    ~Peter


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