473,602 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch a string to an integer

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
8 1406
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*******@disc ussions.microso ft.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
If you expect uppercase letters then how about

intValue = ASC(strLetter) - 64

"El Camino" <an*******@disc ussions.microso ft.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
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
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*******@disc ussions.microso ft.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
Martin,
I would recommend two changes:
Public Function GetNum(ByVal c As Char) As Integer
Return AscW(Char.ToUpp er(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****@ntlworl d.com> wrote in message
news:10******** ********@ersa.u k.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*******@disc ussions.microso ft.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
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*******@disc ussions.microso ft.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
* "El Camino" <an*******@disc ussions.microso ft.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
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.ToUpp er(c)) - 64 > 0 AndAlso AscW(Char.ToUpp er(c)) - 64 < 27
Then
Return AscW(Char.ToUpp er(c)) - 64
Else
Return 0
End if
End Function
///

Just my thought.

Cor
Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
19733
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. Now I find myself wanting to use "Select Case" i.e., "switch" in C# regularly, but I always have to find another way b/c C#'s switch statement only allows static or integral variables. For example, I often want to use a switch statement based on the...
18
1923
by: Torben Laursen | last post by:
Hi Can anyone tell me what is the short-cut to switch between a .h and .cpp file, thanks Torben Laursen ---
4
4973
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if else statements but it looked pretty ugly and the string values to be tested is still increasing. The switch statement seems to be a better choice then the if else statement. But it seems that the switch statement accepts integer values as the test...
7
1677
by: zwasdl | last post by:
When I swtich to design view from data view, the query results are lost, and I have to run again to get it. Is there an option in access to keep the data when I switching views? I know I can copy them to other place, such as excel, but I'd prefer to keep them in access. thanks, Wei
0
1361
by: Sudz28 | last post by:
Greetings! I am attempting to write a program that will allow a user to manipulate data read from a file, and am obviously in no way near finished. However, one problem I'm having that I don't understand is with my switch subroutine. In the below code, if I select "d" or anything that is an invalid response, it loops correctly back to the menu. However, if I attempt to insert (i), find (f), or delete (d) it loops back to the menu *but*...
3
3049
by: Drowningwater | last post by:
I'm writing a program and I've made a switch statement. I have several quesions: 1. I have a string variable and when I try to use that string variable with the switch function I get a compiling error that says: switch quantity not an integer How to I get my switch statement to allow a string variable? 2. Can I make it accept 2 variables (i.e. If i wanted the person to type "go here" each word going into a seperate string variable)? ......
21
7715
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
9
3941
by: Cybex | last post by:
I am trying to get this to work but when ever I enter an proper integer it just hangs. The Switch default seems to catch the improper integers but the right ones are not triggering the way I thought they would. Any help would be appreciated... #include <iostream> #include <string>
27
2539
by: sinbad | last post by:
how to implement a switch case for stringse...switch should happen depending on the strings... thanks sinbad
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.