Being an old MFC guy, I'm having a heck of a time working with MC++. Can
anybody tell me why the following code outputs 65 instead of an A. And yes
I know 65 is the hex value for A, but why is it getting converted? And how
to I get an A to be outputted?
char c = 'A';
StringBuilder *s = new StringBuilder;
s->Append(c);
Console::WriteLine(s);
Thanks
JEK
> Being an old MFC guy, I'm having a heck of a time working with MC++. Can
> anybody tell me why the following code outputs 65 instead of an
[quoted text clipped - 5 lines]
> s->Append(c);
> Console::WriteLine(s);
Because char maps to System.UInt8, not System.Char.
Try
wchar_t c = L'A';
StringBuilder *s = new StringBuilder;
s->Append(c);
Console::WriteLine(s);
-cd
John E Katich - 16 Jun 2005 02:19 GMT
>> Being an old MFC guy, I'm having a heck of a time working with MC++. Can
>> anybody tell me why the following code outputs 65 instead of an
[quoted text clipped - 16 lines]
>
> -cd
Sorry Carl, No help;
This is a simple fragment of the real sitiuation. The <char c> is really
being returned from a COM object, therefore it cannot be defined as anything
other than a char. BTW, your code also yields 65.
JEK
Norman Diamond - 16 Jun 2005 02:35 GMT
>> Being an old MFC guy, I'm having a heck of a time working with MC++.
Considering that ordinary C++ predates MFC (though the C++ standard
doesn't), some of us who saw C++ learn how to choose overloads depending on
char vs. int also wonder why MC++ lacks that ability. Anyway...
>> Can anybody tell me why the following code outputs 65 instead of an A.
>> And yes I know 65 is the hex
Decimal. Hex is 0x41.
>> char c = 'A';
>> StringBuilder *s = new StringBuilder;
>> s->Append(c);
>> Console::WriteLine(s);
>
> Because char maps to System.UInt8, not System.Char.
Last I saw, char maps to System.Int8 or System.Byte or something like that.
Of course C programmers have to take care not to depend on whether char's
values are signed or unsigned, but MC++ programmers often have to find out
which MC++ type is actually being used.
> Try
>
> wchar_t c = L'A';
> StringBuilder *s = new StringBuilder;
> s->Append(c);
> Console::WriteLine(s);
wchar_t maps onto System.UInt16.
Try:
char c = 'A'; // or wchar_t c = L'A'; /// or _TCHAR c = _T('A');
StringBuilder *s = new StringBuilder;
s->Append(Char(c));
Console::WriteLine(s);
I'm not actually sure if this will work. Char('A') worked for me but I
didn't try it with a variable. Char('\0') has a minor bug but works OK at
execution time.
John E Katich - 16 Jun 2005 03:03 GMT
>>> Being an old MFC guy, I'm having a heck of a time working with MC++.
>
[quoted text clipped - 38 lines]
> didn't try it with a variable. Char('\0') has a minor bug but works OK at
> execution time.
Hi Norman,
Char c = 'A' instead of char = 'A' works...
Thanks
JEK
Tomas Restrepo \(MVP\) - 16 Jun 2005 04:00 GMT
Hi Carl,
> Because char maps to System.UInt8, not System.Char.
>
[quoted text clipped - 4 lines]
> s->Append(c);
> Console::WriteLine(s);
Last I remember, that will only work if compiling with /Zc:wchar_t, because
otherwise, wchar_t maps to UInt16 :)

Signature
Tomas Restrepo
tomasr@mvps.org
http://www.winterdom.com/
Carl Daniel [VC++ MVP] - 16 Jun 2005 07:42 GMT
Tomas Restrepo (MVP) wrote:
> Hi Carl,
>
[quoted text clipped - 9 lines]
> Last I remember, that will only work if compiling with /Zc:wchar_t,
> because otherwise, wchar_t maps to UInt16 :)
No doubt. I take it for granted since /Zc:wchar_t is the default in VC
2005.
-cd
> Being an old MFC guy, I'm having a heck of a time working with MC++. Can
> anybody tell me why the following code outputs 65 instead of an A...
[quoted text clipped - 3 lines]
> s->Append(c);
> Console::WriteLine(s);
This is my guess...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemtextstringbuilderclassappendtopic11.asp

Signature
Jeff Partch [VC++ MVP]
John E Katich - 16 Jun 2005 02:28 GMT
>> Being an old MFC guy, I'm having a heck of a time working with MC++. Can
>> anybody tell me why the following code outputs 65 instead of an A...
[quoted text clipped - 6 lines]
> This is my guess...
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemtextstringbuilderclassappendtopic11.asp
Thanks Jeff, that's the answer..
Use Char c = 'A' instead char c = 'A'..
What's interesting is char c = 'A' works in C#
Go figure...
JEK
Carl Daniel [VC++ MVP] - 16 Jun 2005 07:48 GMT
> Thanks Jeff, that's the answer..
>
> Use Char c = 'A' instead char c = 'A'..
>
> What's interesting is char c = 'A' works in C#
char is a keyword in both C++ and C#. In C++, char is defined as a single
byte (the smallest addressible unit of storage). In .NET, this maps to
System.UInt8, while in C#, char is defined as the "platform character type",
or System.Char.
Char, on the other hand, is not a keyword/built-in, but the name of a value
type in the System namespace, so it has the same meaning in both languages.
The solution of declaring variables of type Char won't help your case of
getting character data from a COM object or other legacy code that's
returning character strings using the C/C++ char type. You can get away
with a cast to convert a single char to Char, but to convert strings, you'll
want to use the functions in the System.Runtime.InteropServices.Marshal
class. Specifically, you might want to read up on:
StringToHGlobalAnsi
StringToHGlobalUni
StringToHGlobalAuto
StringToBSTR
PtrToStringAuto
PtrToStringAnsi
PtrToStringUni
PtrToStringBSTR
-cd
John E Katich - 17 Jun 2005 14:30 GMT
>> Thanks Jeff, that's the answer..
>>
[quoted text clipped - 29 lines]
>
> -cd
What about a struct and a pointer to a struct that is returned by a COM
object. What is the best handle them in manaaged code?
JEK
Carl Daniel [VC++ MVP] - 17 Jun 2005 14:48 GMT
> What about a struct and a pointer to a struct that is returned by a
> COM object. What is the best handle them in manaaged code?
One of the great advantages of MC++ is that you can just use unmanaged types
freely.
If you need to pass a native struct to a framework library, or some other
existing IL code (i.e. not MC++ code that you're writing), then you either
need to define a managed equivalent and copy the struct over member by
member (possibly doing string conversions on the way), or you may be able to
define a managed struct that's layout compatible with the unmanaged struct
by using explicit layout attributes on the managed struct.
-cd