Click here to Skip to main content
14,739,758 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: (untagged)
using .net managed code
char a='a';
String^ b=Convert::toString(a);

After above code, b is equal to 97.
How to I convert a char to a string that is not a decimal value?

I want b="a".

I found that a wchar_t type data can convert to it without any problem.

wchar_t a='a';
String^ b=Convert::toString(a);

Must I use wchar_t type to solve my problem?
Updated 7-Sep-10 22:40pm
Dalek Dave 8-Sep-10 3:23am
Edited for Grammar and Code Block.
Emilio Garavaglia 8-Sep-10 3:24am
Reason for my vote of 3
C and C /CLI are two different things. I retagged the question appropriately

Yes, you should use wide characters (because the .NET framework uses them).
As about your example, you may also write:
char a='a';
String^ b=Convert::toString((wchar_t)a)

I don't know what they teach in school these days but I never cease to be amazed at the number of questions from people who appear to have very little understanding of how information is stored in a computer's memory and how it is displayed.

The value 97 is merely the decimal representation of a byte in memory containing the character 'a'. In hex it will appear as x61. The first character of your string will be the character 'a' (97 or x61) and the next character will be a null byte (0 or x0) indicating the end of the string.
Dalek Dave 8-Sep-10 3:48am
Well Explained young man.
CPallini 8-Sep-10 5:38am
I disagree: the first character of the OP string would be 0x0039, the second one 0x0037 (I don't know if .NET strings are internally NULL terminated). The point is (IMHO) that .NET uses UNICODE characters hence, standard C chars are regarded as numbers by the Convert method.
Following Signor Pallini's comments above I have taken another look at the issue and apologise for (some of) my rant, as it should have been directed at Microsoft.

C# (and VB.NET I assume) uses char as an alias for System::Char. C++/CLI uses char as the old C-type character, and uses wchar_t or WCHAR as aliases for System::Char. And as Sgr. P. notes, Convert.ToString(char) takes the char value to be an integer and thus converts it to 97.

Yet another reason why C++/CLI is not a good idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900