Converting "A" to an Integer value

  • Thread starter Thread starter SQL
  • Start date Start date
S

SQL

This is a little more of a logic question. But I'm doing this in VB.NET, so
I figured this is the right place to ask:

I need to figure out the difference between a range that a user will
enter...only they will be entering alphanumeric characters. Like "A" to
"E"...I want to return the difference which would be 5 (A being 1 and E
being 5).

How can I do this?

Thanks
 
You could try this code:

Dim charOne As Char
Dim charTwo As Char

charOne = "E"
charTwo = "a"

'make both lower case in case one is upper and one is lower
charOne = Char.ToLower(charOne)
charTwo = Char.ToLower(charTwo)

Dim intOne As Int32 = Convert.ToInt32(charOne)
Dim intTwo As Int32 = Convert.ToInt32(charTwo)

MessageBox.Show(System.Math.Abs((intOne - intTwo)).ToString())
 
SQL,
Have you tried the AscW function?

Dim c1 As Char = "A"c
Dim c2 As Char = "E"c

Dim diff As Integer = AscW(c2) - AscW(c1)

AscW will give you the Unicode code value for a Char.

Hope this helps
Jay
 
Did I think that I had learned a new commando.

:-)
It's actually 'Asc' or 'AscW', I remember 'ORD' was used in Modula-2...

And if it is this there has to be substracted 65 than afterwards (When you
are sure it are only upercases).

Cor
 
* "CJ Taylor said:
damnit... so it is...

\\\
Public Function Ord(ByVal Character As Char) As Integer
Return Asc(Character)
End Function
///

:->
 
* "Cor Ligthert said:

I hoped that it was possible to define an alias for a method name using
'Imports', something like that:

\\\
Imports Microsoft.VisualBasic.Strings.Asc = Ord
///

But that didn't work ;->.
 
Back
Top