By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

switch a string to an integer

P: n/a
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Try this funtion:
Public Funtion LetterValue(c as char) as integer
if Asc(c) >96 then
return Asc(c) - 96
else
return Asc(c)- 64
end if
End Function

HTH

Jim Mirra
"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

Nov 20 '05 #2

P: n/a
If you expect uppercase letters then how about

intValue = ASC(strLetter) - 64

"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

Nov 20 '05 #3

P: n/a
Got it if anybody is interested..

AscW(strLetter - 64)

will convert to its appropriate number
-----Original Message-----
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx
.

Nov 20 '05 #4

P: n/a
How about...

Public Function GetNum(ByVal c As Char) As Integer
Return Asc(c.ToUpper(c)) - 64
End Function

This handles both upper and lowercase

Regards,

Martin.

"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

Nov 20 '05 #5

P: n/a
Martin,
I would recommend two changes:
Public Function GetNum(ByVal c As Char) As Integer
Return AscW(Char.ToUpper(c)) - 64
End Function
Seeing as Char.ToUpper is a shared method, its more obvious in your code if
you give the Class name (Char) when you call it as oppose to the variable.
Although you can use a Char variable to call the method, it appears that you
are acting on that instance of the variable, when you are not, you are
acting on the parameter...

Also using AscW function will not attempt to translate the char in relation
to the current code page.

Otherwise I think your sample is the "most flexible".

Hope this helps
Jay

"Martin Horn" <mv****@ntlworld.com> wrote in message
news:10****************@ersa.uk.clara.net... How about...

Public Function GetNum(ByVal c As Char) As Integer
Return Asc(c.ToUpper(c)) - 64
End Function

This handles both upper and lowercase

Regards,

Martin.

"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx


Nov 20 '05 #6

P: n/a
Thank you all for your very good advice response
:-)
-----Original Message-----
If you expect uppercase letters then how about

intValue = ASC(strLetter) - 64

"El Camino" <an*******@discussions.microsoft.com> wrote in messagenews:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

.

Nov 20 '05 #7

P: n/a
* "El Camino" <an*******@discussions.microsoft.com> scripsit:
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4


\\\
Dim s As String = "A"
Dim i As Integer = Asc(s) - 64
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
Cor
Hi Martin,

Nice one, to make it more generic I would add

\\\
Public Function GetNum(ByVal c As Char) As Integer
If AscW(Char.ToUpper(c)) - 64 > 0 AndAlso AscW(Char.ToUpper(c)) - 64 < 27
Then
Return AscW(Char.ToUpper(c)) - 64
Else
Return 0
End if
End Function
///

Just my thought.

Cor
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.