0

I have been searching the internet for days about this question. I have made a win32 project in which I want to convert a wstring to a WCHAR array

Please give an example If you find out any mistakes please give an example

wstring timeNow = L"Hello";
WCHAR timeWchar[6] = {(WCHAR)timeNow.c_str()}; // Not Working

Instead of the text I see only a square when I run my program

  • Does this answer your question? [How to convert wstring to wchar\_t\*? C++](https://stackoverflow.com/questions/44985451/how-to-convert-wstring-to-wchar-t-c) – Simon Mourier Aug 07 '21 at 09:13
  • Does it really matter that the WCHAR array is 6 large? Since you are creating an array, you need to copy over the values into it. You could also use `const WCHAR *timeWchar = timeNow.c_str();` that way, you can also use functions that take `const WCHAR [ ]` arguments without the need to copy. – JVApen Aug 07 '21 at 09:20
  • 2
    Your code fails because the cast tells a compiler a lie. A pointer is not a WCHAR. That's why the compiler objected, and why the cast allowed the code to compile but not do anything useful. A question for me is what you are doing with this array? As JVApen says perhaps you don't need the array at all depending on what the code that uses it is actually doing. We can't see that unfortunately. – David Heffernan Aug 07 '21 at 10:05
  • I left you an answer below showing possibilities of initializing an array from a string. But it occurred to me, just like the comments above that I didn't originally read, that copying a string into an array may not be needed at all. There's a lot of great `std::` classes in C++ that would probably be better than using a string copy into an array for the task at hand. Can you open up more about what you are really trying to do? I'll amend my answer if I have more details from you. – selbie Aug 07 '21 at 17:13

3 Answers3

0

I assume that by WCHAR, you mean wchar_t.

You can loop over the array and assign the elements.

Or if you don't feel like writing the loop yourself, you can use an algorithm from the standard library. Example:

assert(timeNow.size() < 6);
wchar_t timeWchar[6] {};
std::ranges::copy(timeNow, timeWchar);
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

You can't really initialize an array with a pointer.

This is closer to what you want without making a copy of the string into an array. just use a pointer to reference the chars.

wstring timeNow = L"Hello";
const WCHAR* timeWchar = timeNow.c_str();

99% of the time, the above works for whatever you need to do assuming you don't need to modify the chars.

If really need to make a make a copy of the characters into a different array, such as when you need to manipulate the string, this will do in Windows just fine - assuming you have a fixed size array that is big enough to receive the copy.

wstring timeNow = L"Hello";
WCHAR timeWchar[6];
StringCchCopyW(timeWchar, ARRAYSIZE(timeWchar), timeNow.c_str());

If you don't know the length ahead of time, then you'll need to allocate it before making the copy:

size_t allocSize = timeNow.size() + 1;
WCHAR timeWchar = new WCHAR[allocSize];
StringCchCopyW(timeWchar, allocSize, timeNow.c_str());
/* don't forget to `delete [] timeWchar` when you are done */

But then again why mess with new and delete when you can just let C++ do the work for you. Make a copy of the string and then use a pointer to reference the characters in the copy.

wstring timeNowCopy = timeNow;
const WCHAR* timeWchar = timeNowCopy.c_str(); // timeWchar points to the array copied into timeNowCopy. 

If you can maintain the lifetime of timeNowCopy and timeWchar on the stack together, then you don't need to explicitly new or delete anything.

selbie
  • 100,020
  • 15
  • 103
  • 173
-2

just do its .data() .

WCHAR timeWchar[6] = ...

wont work anyway

in which I want to convert a wstring to a WCHAR array

if you mean wchar_t [6] type - you just do not need this. you don't have a situation where you can go with wchar_t [6] but can't with wstr.data() .

  • 1
    *"you don't have a situation where you can go with `wchar_t [6]` but can't with `wstr.data()`" - That's incorrect. `wstr.data()` loses the size information, that's still captured in the array. – IInspectable Aug 07 '21 at 11:13