Hello,
<dumb question>
I am trying to do something similar to the following...
string str = "Hello";
char c = (char)str.Substring(2,1);
but the compiler complains that it cannot convert a string to a char.
How do I do this? I know that c will only be one character long, so it
can be converted to a char, but the compiler doesn't know that.
If it's of any help, I'm converting it to a char in order to get the
ASCII value, so if there's an easier way of converting a one-character
substring of a string to an int, please tell me.
Thanks
</dumb question>
--
Alan Silver
(anything added below this line is nothing to do with me) 5 2902
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:Wv**************@nospamthankyou.spam... string str = "Hello"; char c = (char)str.Substring(2,1);
but the compiler complains that it cannot convert a string to a char.
Have you tried Convert.ToChar(...) ?
Hi Alan,
First, remember that a string is an array of char. The string object is a
wrapper for an array of char. So, you can simply do the following:
string str = "Hello";
char c = str[2]; // c = 'l'
Then you can cast the char as an int with the following:
int i = (int)c;
Or, to get really elegant,
int i = (int)"Hello"[2];
- or -
int i = (int)str[2];
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:Wv**************@nospamthankyou.spam... Hello,
<dumb question>
I am trying to do something similar to the following...
string str = "Hello"; char c = (char)str.Substring(2,1);
but the compiler complains that it cannot convert a string to a char.
How do I do this? I know that c will only be one character long, so it can be converted to a char, but the compiler doesn't know that.
If it's of any help, I'm converting it to a char in order to get the ASCII value, so if there's an easier way of converting a one-character substring of a string to an int, please tell me.
Thanks
</dumb question> -- Alan Silver (anything added below this line is nothing to do with me)
>Hi, what if you try like this:
char c=str[2];
Thanks, I discovered that just after posting!!
I have found an infallible way to get the answers you need. First search
the archives and get frustrated that you can't find it, then post a
message here and IMMEDIATELY go and search the archives again. Doesn't
matter if you use the same keywords as before, you're guaranteed to find
the answer straight away!!
Thanks for the reply.
--
Alan Silver
(anything added below this line is nothing to do with me)
>Hi Alan,
Hi Kevin,
Thanks for the reply. As I mentioned in another reply (my own to
myself!!), I discovered this shortly after posting.
I think I am going to investigate the quantum effects of posting a
question in Usenet on the probability of finding the answer within a
specified number of picoseconds after the message has been sent. My
observations (which according to the laws of quantum physics must bias
the effect) indicate that there is a strong correlation here.
I could write a PhD thesis on this, then sell it to Google and retire on
the profits!! Ha ha.
Ta ra First, remember that a string is an array of char. The string object is a wrapper for an array of char. So, you can simply do the following:
string str = "Hello"; char c = str[2]; // c = 'l'
Then you can cast the char as an int with the following:
int i = (int)c;
Or, to get really elegant,
int i = (int)"Hello"[2];
- or -
int i = (int)str[2];
--
Alan Silver
(anything added below this line is nothing to do with me) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Trep |
last post by:
Hi there!
I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.
This is necessary because I...
|
by: IamZadok |
last post by:
Hi
I was wondering if anyone knew how to convert a string or an integer
into a Static Char.
Thx
|
by: MilanB |
last post by:
Hello
How to convert char to int?
Thanks
|
by: Yifan |
last post by:
Hi
Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks
Yifan
|
by: Brian Henry |
last post by:
first question... I have a flat file which unfortinuatly has columns
seperated by nulls instead of spaces (a higher up company created it this
way for us) is there anyway to do a readline with this...
|
by: Steve Kershaw |
last post by:
I can convert from a string to a char array (char) by using
string.ToCharArray() function. Now how do I convert it back?
The string looks like this: "000457". I've converted it to a char
array:...
|
by: Zytan |
last post by:
I am surprised that a single character string is not auto-created from
a single char. It is hard to find information on converting a char
into a string, since most people ask how to convert char...
|
by: Man4ish |
last post by:
HI ,
I am trying to convert string into char array of characters.but facing problem.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string t="1,2,3,4,5,6";
|
by: Peter |
last post by:
Trying to convert string to byte array.
the following code returns byte array of {107, 62, 194, 139, 64}
how can I convert this string to a byte array of {107, 62, 139, 65}
...
|
by: rtillmore |
last post by:
Hello,
I did a quick google search and nothing that was returned is quite
what I am looking for. I have a 200 character hexadecimal string that
I need to convert into a 100 character string.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |