• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8581
  • Last Modified:

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
0
nzfire
Asked:
nzfire
  • 4
  • 4
1 Solution
 
Jaime OlivaresCommented:
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 OlivaresCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nzfireAuthor Commented:
Oops, you posted the second comment just as I was posting my reply.
0
 
Jaime OlivaresCommented:
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 OlivaresCommented:
Dim Character As String
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now