Robert Beaubien
Robert Beaubien

Reputation: 3156

What's the equivalent of VB's ASC() function?

I have the following code that I need to get rid of the Microsoft.VisualBasic function ASC():

Dim keyWithoutDashes As String = p_licenseKey.Replace("-", "").Trim.ToLower
Dim checksum As Int32 = 0
Dim modValue As Byte
Dim checkValue As Byte
Dim checkChar As Char
checksum = 0
checkValue = CByte(Asc(keyWithoutDashes.Substring(24, 1)))
For pos = 0 To 23
    checksum += CByte(Asc(keyWithoutDashes.Substring(pos, 1)))
Next
checksum = checksum Mod 34
modValue = CByte(Asc(VALID_CHARACTERS_BACKWARD.Substring(checksum, 1)))
If modValue <> checkValue Then
    m_keyErrorMessage = "Invalid LicenseKey"
    Return False
End If

I have tried casting as int with no success. Any suggestions?

I need to make the code work in a Microsoft Portable Class Library.

Upvotes: 1

Views: 837

Answers (1)

Robert Beaubien
Robert Beaubien

Reputation: 3156

The answer thanks to Hans Passant's comment above:

checksum = 0
checkValue = Encoding.ASCII.GetBytes(keyWithoutDashes.Substring(24, 1))(0)
For pos = 0 To 23
  checksum += Encoding.ASCII.GetBytes(keyWithoutDashes.Substring(pos, 1))(0)
Next
checksum = checksum Mod 34
modValue = Encoding.ASCII.GetBytes(
  VALID_CHARACTERS_BACKWARD.Substring(checksum, 1))(0)

If modValue <> checkValue Then
  m_keyErrorMessage = "Invalid LicenseKey"
  Return False
End If

Upvotes: 1

Related Questions