473,769 Members | 6,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I convert a string to a char?

Hello,

<dumb question>

I am trying to do something similar to the following...

string str = "Hello";
char c = (char)str.Subst ring(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)
Nov 19 '05 #1
5 2924
Hi,

what if you try like this:

char c=str[2];
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #2
"Alan Silver" <al*********@no spam.thanx> wrote in message
news:Wv******** ******@nospamth ankyou.spam...
string str = "Hello";
char c = (char)str.Subst ring(2,1);

but the compiler complains that it cannot convert a string to a char.


Have you tried Convert.ToChar( ...) ?
Nov 19 '05 #3
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*********@no spam.thanx> wrote in message
news:Wv******** ******@nospamth ankyou.spam...
Hello,

<dumb question>

I am trying to do something similar to the following...

string str = "Hello";
char c = (char)str.Subst ring(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)

Nov 19 '05 #4
>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)
Nov 19 '05 #5
>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)
Nov 19 '05 #6

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

Similar topics

27
51732
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 have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
5
6194
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
7
66736
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
10833
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
12
2989
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 and not have it affected by the null? because it is right now causes truncated data at wierd places... but as soon as i manually with a hex editor change char(00) to char(20) in the files it reads prerfectly... which leads me to my 2nd...
2
2354
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: char = {0, 0, 0, 4, 5, 7}. Now I need to convert it back to a string. Thanks in advance for your help. Steve
5
38539
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 to string. I have a function that accepts a string, and I wish to access this function for each character in the string, individually. The only solution I have found is: char c = 'A'; string s = System.Text.Encoding.ASCII.GetString(c);
4
17375
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";
12
13553
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} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
14
13536
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. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
9589
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
9423
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
10045
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
8870
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...
1
7408
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
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
5298
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
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.