Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert char* to wchar_t*?

I've tried implementing a function like this, but unfortunately it doesn't work:

const wchar_t *GetWC(const char *c) {     const size_t cSize = strlen(c)+1;     wchar_t wc[cSize];     mbstowcs (wc, c, cSize);      return wc; } 

My main goal here is to be able to integrate normal char strings in a Unicode application. Any advice you guys can offer is greatly appreciated.

like image 537
AutoBotAM Avatar asked Nov 07 '11 02:11

AutoBotAM


People also ask

What is a wchar_t in C++?

The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.

What is the difference between Wchar and char?

Wide characters are similar to character datatype. The main difference is that char takes 1-byte space, but wide character takes 2-bytes (sometimes 4-byte depending on compiler) of space in memory. For 2-byte space wide character can hold 64K (65536) different characters. So the wide char can hold UNICODE characters.


1 Answers

In your example, wc is a local variable which will be deallocated when the function call ends. This puts you into undefined behavior territory.

The simple fix is this:

const wchar_t *GetWC(const char *c) {     const size_t cSize = strlen(c)+1;     wchar_t* wc = new wchar_t[cSize];     mbstowcs (wc, c, cSize);      return wc; } 

Note that the calling code will then have to deallocate this memory, otherwise you will have a memory leak.

like image 141
Andrew Shepherd Avatar answered Oct 08 '22 18:10

Andrew Shepherd