This question already has answers here:
Convert char[] to LPCWSTR
(5 answers)
Closed 6 years ago.
SetTextColor(hdc, RGB(0, 0, 0));
char str[20]="";
sprintf(str, "sorce: %d", sorce);
TextOut(hdc, 930, 810, str,strlen(str));
It showed the error that char* cannot be converted to LPCWSTR. How can I solve it?
The W in LPCWSTR means wide characters. You have a few options.
You can change from using char types to wchar_t
You can convert using codecvt or Win32 API
Use TextOutA to keep using char types.
Related
This question already has answers here:
How to convert string to wstring in C++
(1 answer)
How to convert char* to wchar_t*?
(9 answers)
How to convert UTF-8 std::string to UTF-16 std::wstring?
(6 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
If I have this below code -
string str;
wstring wstr;
for (char x : str)
{
wstr += x;
}
Is this line wstr += x wrong? Do I need some conversion function to convert char x to wchar_t to be stored in wstr? If yes, which conversion function do I need?
Edit - I have gone through the linked answers, it mentions about converting the array of wchar_t and char -> for that for sure conversion functions are needed but my question specifically asks if a char -> wchar_t would need conversion function
This question already has answers here:
C++ Convert string (or char*) to wstring (or wchar_t*)
(19 answers)
Closed 2 years ago.
To convert a string literal to wchar_t we can use L like:
wchar_t variable[10] = L"some text";`
But if the string is stored inside a variable then how do I convert it to wchar_t?
Suppose the string is in a variable
string varString="someText";
I want to store it in a variable of type wchar_t, for example wchar_t var;
How do I type cast and store it?
I want to place the variables inside a loop where their values will change with each cycle:
for(i=0;i<10;i++)
{
var=(*some kind of casting*)varString;
}
Two different ways...depending on simplicity... This is in Visual C++ group...
So first I would try using CStringW. Depending on #defines your regular CString might be a CStringA or CStringW. But, you can say CStringW.
CStringW sWide = "abcdef"; // uses current thread code page
const wchar_t* pWide = sWide.GetString(); // pointer only valid for scope of sWide
Or you can use MultiByteToWideChar() API.
wchar_t wszBuf[512];
MultiByteToWideChar(CP_ACP, 0, "abcdef", 6, wszBuf, _countof(wszBuf)); // substitute "abcdef" and the 6 (length) for your usage...
This question already has answers here:
vc++ - How to convert a CString into LPCWSTR
(6 answers)
Closed 6 years ago.
I have a function which retrieves resources from .rc file using CStringW.
I want use this returned value in sprintf_s.Is there any way?
//Snippet
sprintf_s(szMsgBoxText, LoadFromResource(IDS_INSTALLATION_COMPLETE), g_szProductName);
CStringW LoadFromResource(int ID)
{
CStringW m_resoucestring(MAKEINTRESOURCE(ID));
return m_resoucestring;
}
sprintf_s giving me an error.Is there any alternative for this?
Use CStringW::Format which does exactly the same as sprintf_s. So your code will become:
CStringW sText;
sText.Format(LoadFromResource(IDS_INSTALLATION_COMPLETE), g_szProductName);
or even
CStringW sText;
sText.Format(IDS_INSTALLATION_COMPLETE, g_szProductName);
You can pass CStringW directly to any function that accepts LPCWSTR type of parameter as it has corresponding cast operator.
This question already has answers here:
std::string formatting like sprintf
(44 answers)
Closed 7 years ago.
How can i use std::wstring variable as argument in to swprintf_s instead wchar_t *?
My code is as below but has error:
#include <string>
#include <iostream>
using namespace std;
int main(){
std::wstring msg(20, 0);
swprintf_s( msg.c_str(), 20, L"This is a test for using std::wstring");//this line has error
wcout<< msg<< endl;
return 0;
}
swprintf or swprintf_s writes to the memory pointed by its first parameter. Therefore that parameter cannot be a const char or wchar pointer. But the return value of std::wstring::c_str() is const char* . So you cannot just cast way the const. As mentioned by Jason, if you break the rule, wstring::size() may not return the right value.
What you can do is
wchar_t buffer[20];
swprintf_s(buffer, 20, L"This is a test for using std::wstring");
If you want a std::wstring, you can just use the constructor, like wstring msg(buffer).
This question already has answers here:
Convert int to LPCWSTR by using wsprintf
(1 answer)
Display a Variable in MessageBox c++
(5 answers)
Closed 8 years ago.
I have a proplem , like :
Ex :
MessageBoxW(0,L"Đây là ABC (This is ABC)",L"Lỗi (Error)",0);
All ok !
But i want to replace ABC to variable , like it :
char buff[500];
char author[] = "ABC";
sprintf_s(buff,"Đây là %s (This is %s)",author);
MessageBoxW(0, WHAT WILL BE HERE,L"Lỗi (Error)",0);
I hope someone may help !
You can certainly display a variable, but it has to be of the correct type. MessageBoxW takes a LPCWSTR (wide), and a char[] provides a LPCSTR (narrow) instead. So swap out the types accordingly:
WCHAR buff[500]; // WCHAR not char
WCHAR author[] = L"ABC"; // WCHAR not char
swprintf_s(buff, L"Đây là %s (This is %s)", author); // swprintf_s not sprintf_s
MessageBoxW(0, buff, L"Lỗi (Error)", 0);
It's also a good idea to avoid the raw buffers and use a wrapper class such as ATL::CStringW or std::wstring.
(I had some trouble deciding whether to answer this. The related question Why can't I display this string on MessageBox? seems like a duplicate, but it's closed as a duplicate of Cannot convert parameter from 'const char[20]' to 'LPCWSTR' which does not answer this question. In fact its answer is included in this question.)