# How do you convert from 'const wchar_t *' to 'WCHAR [32]'

## Recommended Posts

Hi, Trying to figure out a conversion. I am passing in a std::wstring refrence to a function and then trying to assign it to a D3DXFONT_DESC member. int CreateFont(std::wstring& name) { D3DXFONT_DESC lf. lf.FaceName = name.ctr(); } This gives error C2440: '=' : cannot convert from 'const wchar_t *' to 'WCHAR [32]' Any suggestions? Regards Chad

##### Share on other sites
Fox32    139
You need to memcpy or strcpy the string from name.c_str() to the wchar_t array. Remember that lf.FaceName must be a null terminated string!

##### Share on other sites
Windryder    270
Quote:
 Original post by Fox32You need to memcpy or strcpy the string from name.c_str() to the wchar_t array. Remember that lf.FaceName must be a null terminated string!

You will probably not want to use strcpy() since it is not designed to handle multibyte strings. memcpy() will not null-terminate for you. According to MSDN the function wcscpy() can be used; it has the same signature as strcpy() but takes widechar arguments.

EDIT: And obviously you need to be careful not to overflow the buffer too. :)

##### Share on other sites
Fox32    139
Quote:
 And obviously you need to be careful not to overflow the buffer too. :)

wcscpy_s would solve this problem.

Edit:

errno_t result = wcscpy_s(lf.FaceName, name.ctr());

If result is non zero, your name is probably longer than 31 characters and than lf.FaceName isn't modified.

##### Share on other sites
Excuse me if I am a dope but I am trying this and it is not working.

int CreateFont(std::wstring& name)
{
D3DXFONT_DESC lf.
assert(fontName.length() < sizeof(lf.FaceName));
strncpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str(), fontName.length() + 1);
}

error C2664: 'errno_t strncpy_s(char *,rsize_t,const char *,rsize_t)' : cannot convert parameter 1 from 'WCHAR [32]' to 'char *'

Regards

##### Share on other sites
Hmm this seem sto be compiling fine now but I am worried about overflow

int CreateFont(std::wstring& name)
{
D3DXFONT_DESC lf.
assert(fontName.length() < sizeof(lf.FaceName));
wcscpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str());
}

how would you suggest I check for them.

Regards

##### Share on other sites
Fox32    139
Quote:
 Original post by chadsxeerror C2664: 'errno_t strncpy_s(char *,rsize_t,const char *,rsize_t)' : cannot convert parameter 1 from 'WCHAR [32]' to 'char *'

If you use wide strings, you need to use the wide string version of strncpy_s called wcsncpy_s.

##### Share on other sites
Do you feel like the assert if enough of a check for overflow?

Regards

##### Share on other sites
SiCrane    11839
That depends on where the string is coming from. If it's chosen by the user, definitely not.

##### Share on other sites
Quote:
 Original post by SiCraneThat depends on where the string is coming from. If it's chosen by the user, definitely not.

It is chosen by the user. What steps can I take to assure that the user is under or equal to or less then 31 characters.

Regards

##### Share on other sites
Codeka    1239
Quote:
 Original post by chadsxeint CreateFont(std::wstring& name){D3DXFONT_DESC lf.assert(fontName.length() < sizeof(lf.FaceName));wcscpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str());}
sizeof returns the size in bytes, so if lf.FaceName is a wide string, the length of the string is actually half of what sizeof returns. But actually, you probably don't even need to worry about that. Try something like this:

int CreateFont(std::wstring &name){  D3DXFONT_DESC lf;  wcscpy_s(lf.FaceName, name.c_str());}
A few things to point out:

The second parameter to wcscpy_s is only needed if you don't know at compile-time the size of the buffer. Otherwise, some template magic is used to insert the correct value, and that's what I'm doing here.

In this case, you can probably get away with simply truncating the string if it's too long. You've really got to consider it on a case-by-case basis, though. That's the difficulty of working with C APIs, unfortunately... (wcscpy_s will truncate the string if it's too long).

##### Share on other sites
Codeka    1239
Quote:
 Original post by chadsxeint CreateFont(std::wstring& name){D3DXFONT_DESC lf.assert(fontName.length() < sizeof(lf.FaceName));wcscpy_s(lf.FaceName, sizeof(lf.FaceName), fontName.c_str());}
sizeof returns the size in bytes, so if lf.FaceName is a wide string, the length of the string is actually half of what sizeof returns. But actually, you probably don't even need to worry about that. Try something like this:

int CreateFont(std::wstring &name){  D3DXFONT_DESC lf;  wcscpy_s(lf.FaceName, name.c_str());}
A few things to point out:

The second parameter to wcscpy_s is only needed if you don't know at compile-time the size of the buffer. Otherwise, some template magic is used to insert the correct value, and that's what I'm doing here.

In this case, you can probably get away with simply truncating the string if it's too long. You've really got to consider it on a case-by-case basis, though. That's the difficulty of working with C APIs, unfortunately... (wcscpy_s will truncate the string if it's too long).