Click to See Complete Forum and Search --> : Unicode String*


joonas
March 28th, 2008, 06:16 PM
I got a String* that is full of data, where the original wchar_t* in a file has been read as char, so what I really need to do is recreate the String* but treat every two chars as one.

How can I do this in managed C++?

darwen
March 29th, 2008, 05:29 AM
So you want to create a string from a char * array ?


char *array = getANSIString();
String *string = new String(array);


in Managed C++, or


char *array = getANSIString();
String ^string = gcnew String(array);


in C++/CLI.

Darwen.

joonas
March 30th, 2008, 05:03 AM
Well, that will only recreate the String I already have.

The data is read from a file and is in fact an array of wchar_t, but the one who read the data read it as chars, so my string is just a bunch of weird characters. Now I want to put this back into a wchar_t array.
In C++ I would just cast the char* array to a wchar_t* and it would be fine, but how do I do this in Managed C++ and get my data as a wchar_t*?

darwen
March 30th, 2008, 05:35 AM
If you're just trying to read a unicode text file in you can use System::IO::File::ReadAllText to read the file.

However I bet it doesn't have the 0xfffe marker at the start to mark it as a unicode file does it ?

Therefore using the System::String method I've given is as good as any method.

But if you have the unicode as a char * array you can just cast it and do :


char *array = readFile();
const wchar_t *unicode = reinterpret_cast<const wchar_t *>(array);
System::String ^myString = gcnew System::String(unicode);


Darwen.