Help! : single char to String^

P

Peteroid

The following code:

char x_char = '0' ;
String^ x_str = c_char.ToString( ) ;

results in:

x_str = = "48"

and not:

x_str == "0"

[NOTE: the ASCII code for char('0') is int(48)]

MS is aware of this, and has given no guarantees it will ever be fixed.
ToString( ) also has worse problems with char arrays according to a filed
bug report (which says it always returns the exact same string:
"String.Char[]", and MS replied that to modify this behavior would be a
breaking change. HUH? Links after signature if interested in reading about
it).

Help! I need the following function:

String^ Char_To_String( char c )
{
// ???
}

That if given c == '0' returns "0". Thanks for responses! : )

[==P==]

The links to the bug reports:

http://lab.msdn.microsoft.com/produ...edbackid=788dfe28-0ac8-4c5f-96c2-bb5065fd7c2b

http://lab.msdn.microsoft.com/produ...edbackid=8e62476e-599a-44cf-81db-d6a026770ad3
 
C

Carl Daniel [VC++ MVP]

Peteroid said:
The following code:

char x_char = '0' ;
String^ x_str = c_char.ToString( ) ;

results in:

x_str = = "48"

and not:

x_str == "0"

[NOTE: the ASCII code for char('0') is int(48)]

MS is aware of this, and has given no guarantees it will ever be
fixed. ToString( ) also has worse problems with char arrays according to a
filed bug report (which says it always returns the exact same string:
"String.Char[]", and MS replied that to modify this behavior would be
a breaking change. HUH? Links after signature if interested in reading
about it).

char is a signed byte - equivalent to the .NET System.Int8 type. In the
eyes of .NET, it is NOT a char.

wchar_t is a 16-bit character and is equivalent to the .NET System.Char
type.

So:

String^ Char_To_String( char c )
{
return static_cast<wchar_t>(c).ToString();
}
 
P

Peteroid

Thanks, Carl, that did the trick! : )

[==P==]

Carl Daniel said:
Peteroid said:
The following code:

char x_char = '0' ;
String^ x_str = c_char.ToString( ) ;

results in:

x_str = = "48"

and not:

x_str == "0"

[NOTE: the ASCII code for char('0') is int(48)]

MS is aware of this, and has given no guarantees it will ever be
fixed. ToString( ) also has worse problems with char arrays according to
a
filed bug report (which says it always returns the exact same string:
"String.Char[]", and MS replied that to modify this behavior would be
a breaking change. HUH? Links after signature if interested in reading
about it).

char is a signed byte - equivalent to the .NET System.Int8 type. In the
eyes of .NET, it is NOT a char.

wchar_t is a 16-bit character and is equivalent to the .NET System.Char
type.

So:

String^ Char_To_String( char c )
{
return static_cast<wchar_t>(c).ToString();
}
 
Top