467,879 Members | 1,200 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

convert (single) char to string

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);

But, using the Encoding functions??? This seems so wrong, since it
doesn't have to convert anything. I could get demons from this, since
it could change the underlying 16-bit value stored within the char.
ASCII isn't good enough. Default uses the default code page, whatever
that may be. What can I do to ensure 100% that it stays the same?

Debug.Assert(s[0] == c);

Zytan

Mar 2 '07 #1
  • viewed: 37617
Share:
5 Replies
I think the answer that works 100% of the time is:

char c = 'A';
string s = c.ToString();
Debug.Assert(s[0] == c);

Zytan

Mar 2 '07 #2

"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
>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);
how about:

char c = 'A';
string s = c.ToString();

?
Mar 2 '07 #3
how about:
>
char c = 'A';
string s = c.ToString();

?
Yup, that's what I'm thinking.

Thanks,

Zytan

Mar 3 '07 #4
Zytan wrote:
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);

But, using the Encoding functions??? This seems so wrong, since it
doesn't have to convert anything. I could get demons from this, since
it could change the underlying 16-bit value stored within the char.
ASCII isn't good enough. Default uses the default code page, whatever
that may be. What can I do to ensure 100% that it stays the same?

Debug.Assert(s[0] == c);

Zytan
Using encoding to convert a char to a string is not correct. Then you
will be converting the char into a byte and treating that byte as if it
was encoded data that you can decode into a string.

There are several ways of turning a char into a string:

char c = 'A';

string s1 = new String(c, 1);
string s2 = new String(new char[] { c });
string s3 = c.ToString();
--
GŲran Andersson
_____
http://www.guffa.com
Mar 3 '07 #5
Using encoding to convert a char to a string is not correct. Then you
will be converting the char into a byte and treating that byte as if it
was encoded data that you can decode into a string.
That's what I thought.
There are several ways of turning a char into a string:

char c = 'A';

string s1 = new String(c, 1);
string s2 = new String(new char[] { c });
string s3 = c.ToString();
Ok, thanks

Zytan

Mar 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by sposes | last post: by
2 posts views Thread by Steve Kershaw | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.