Click here to Skip to main content
15,867,453 members

How can I convert/(cast) my "string" values to characters in the following C++ example?

RedDk asked:

Open original thread
C++
#using <System.dll>
using namespace System;

int main()
{
	//Two arrays: __wchar_t and String
	array<__wchar_t^>^ chararray3 = gcnew array<__wchar_t^>(5);
	array<String^>^ strarray = gcnew array<String^>(5);
	//values into arrays
	chararray3[0] = L'\u2550';
	chararray3[1] = L'\u2551';
	chararray3[2] = L'\u2552';
	chararray3[3] = L'\u2553';
	chararray3[4] = L'\u2554';
	// note: that compiler generates warning C4566 when generating next array "cannot be represented in current code page (1252)"
	strarray[0] = "\u2550";
	strarray[1] = "\u2551";
	strarray[2] = "\u2552";
	strarray[3] = "\u2553";
	strarray[4] = "\u2554";
	int cp;
	// __wchar_t array contents to console ...
	for(cp=0;cp<5;cp++)
	{
		Console::WriteLine(chararray3[cp]);
	}
	Console::WriteLine(" ");					// space
	// String array contents to console ... expected result with subbstitution character "?" due to code-page-error/other
	for(cp=0;cp<5;cp++)
	{
		Console::WriteLine(strarray[cp]);
	}
	// Question is: why isn't THIS gonna happen ...  ????
	for(cp=0;cp<5;cp++)
	{
		Console::WriteLine((__wchar_t) strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
	}
	/* wth ... converting on the fly! 
	
		Why not! It's a string but it's also a literal unicode value identical to the ones 
		contained in the array of __wchar_t that expands to console value without complaint in the above!?
	*/
	return 0;
}
Tags: C++/CLI, Managed, CLR

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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