NMEA Checksum in VB6

HI,

I have the following code from the web that is in VB.Net and I am writing an app in vb6...Yes I know, I should be moving on! haha!

Could anyone give me a hand with converting it to vb6. I have a problem with the CByte vb6 function:

The following is the .Net code:

'----------------------
  ' Returns True if a sentence's checksum matches the calculated checksum
  Public Function IsValid(ByVal sentence As String) As Boolean
    ' Compare the characters after the asterisk to the calculation
    Return sentence.Substring(sentence.IndexOf("*") + 1) = GetChecksum(sentence)
  End Function

  ' Calculates the checksum for a sentence
  Public Function GetChecksum(ByVal sentence As String) As String
    ' Loop through all chars to get a checksum
    Dim Character As Char
    Dim Checksum As Integer
    For Each Character In sentence
      Select Case Character
        Case "$"c
          ' Ignore the dollar sign
        Case "*"c
          ' Stop processing before the asterisk
          Exit For
        Case Else
          ' Is this the first value for the checksum?
          If Checksum = 0 Then
            ' Yes. Set the checksum to the value
            Checksum = Convert.ToByte(Character)
          Else
            ' No. XOR the checksum with this character's value
            Checksum = Checksum Xor Convert.ToByte(Character)
          End If
      End Select
    Next
    ' Return the checksum formatted as a two-character hexadecimal
    Return Checksum.ToString("X2")
  End Function
'-------------------


Thanks,

Sean
LVL 1
nzfireAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
Function GetChecksum(ByVal sentence As String) As String
    ' Loop through all chars to get a checksum
    Dim Character As Char
    Dim Checksum As Integer
    Dim I as Integer
    For I = 1 to Len(sentence)
      Character = Mid(sentence, I, 1)
      Select Case Character
        Case "$"
          ' Ignore the dollar sign
        Case "*"
          ' Stop processing before the asterisk
          Exit For
        Case Else
          ' Is this the first value for the checksum?
          If Checksum = 0 Then
            ' Yes. Set the checksum to the value
            Checksum = Asc(Character)
          Else
            ' No. XOR the checksum with this character's value
            Checksum = Checksum Xor Asc(Character)
          End If
      End Select
    Next
    ' Return the checksum formatted as a two-character hexadecimal
   GetChecksum = Hex(CheckSum)
   If Len(CheckSum)<2
      CheckSum = "0" & CheckSum
   End If
End Function
0
 
Jaime OlivaresSoftware ArchitectCommented:
Function IsValid(ByVal sentence As String) As Boolean
    ' Compare the characters after the asterisk to the calculation
    Return Mid(sentence, Instr(sentence,"*") + 1) = GetChecksum(sentence)
End Function

I think that's all, but I have not tested
0
 
nzfireAuthor Commented:
Hi Jaime,

Looks really good thanks. I had something very similar, it was just converting to a hex at the end.

There is another small function posted above that code. Are you able to look at it?

Cheers,
Sean
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
nzfireAuthor Commented:
Oops, you posted the second comment just as I was posting my reply.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Sorry, at the end of first function, must be:
   ......
   GetChecksum = Hex(CheckSum)
   If Len(GetChecksum) <2
      GetChecksum = "0" & GetChecksum
   End If
End
0
 
nzfireAuthor Commented:
Hi Jaime,

Just testing your code now. Char is a .Net type. What is the vb equivalent? Or rather, how could I substitute Char in VB6 as it is a new type in .Net?

Thanks again,

Sean
0
 
nzfireAuthor Commented:
Jaime,

Argh, I should try things before asking questions!!! Sorted it now, thanks heaps for the help.

Sean
0
 
Jaime OlivaresSoftware ArchitectCommented:
Dim Character As String
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.