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
Related
This question already has answers here:
C++ concat three char* strings togther [closed]
(4 answers)
Closed 2 years ago.
When I am trying to run this code. I'm getting this error "C2110: '+' : cannot add two pointers". Can anyone just tell me what is wrong in the code?
string Msg;
getline(cin, Msg);
string output;
output = "<Rvc>\n"+"<Msg>"+Msg+"< / Msg>\n";
C-style string literals are not std::strings. "<Rvc>\n" and "<Msg>" are of type const char[] and could decay to pointers (i.e. const char*). Adding on pointers doesn't make sense.
You can just
output = "<Rvc>\n<Msg>"+Msg+"< / Msg>\n";
Then the overloaded operator+ for std::string taking const char* and std::string will be used.
This question already has answers here:
How to convert a string of hex values to a string?
(4 answers)
Converting a hex string to a byte array
(22 answers)
Closed 4 years ago.
I have a string like this:
std::string s="840D8E88B0AC";
and an array:
char MAC[6];
I want to produce this:
MAC={0x84,0x0D,0x8E,0x88,0xB0,0xAC};
I try with sscanf() but I can't make it.
sscanf(s.c_str(), "%02X%02X%02X%02X%02X%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
It should be (other errors notwithstanding)
sscanf(s.c_str(), "%02X%02X%02X%02X%02X%02X", &MAC[0], &MAC[1], &MAC[2],
&MAC[3], &MAC[4], &MAC[5]);
sscanf (and variants) require pointers in order to change the variables that are being read into.
Surprised your compiler didn't warn you about that error.
This question already has answers here:
C or C++. How to compare two strings given char * pointers?
(8 answers)
Closed 4 years ago.
If I have a char* that is the output of another function, and I know it is one of the 10 known words, what is the best way to find what is it?
converting the char* to string bystd::string(char*) , then using string.compare() ?
char* c = "hi";
string s = std::string(c);
if (s.compare("hello") )
Is this the best way? I can not directly write:
char* c ="hi";
if(c == "hello")
Since you already have a C string, just use strcmp. It will likely be faster than the s.compare method since you avoid the overhead of doing a conversion to std::string for both the original string and the string to compare to.
if (strcmp(c, "hello") == 0) {
...
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.)
This question already has an answer here:
How to convert 'wchar_t *' to 'const char *'
(1 answer)
Closed 9 years ago.
I am a kind of new for c++ , while working on the windows CE .net compact application
while trying to write hexa datas to a file
CString dataBlock1;
dataBlock1 = "";
CString temp;
for(int i = 0; i < rLen; i++)
{
temp.Format(L"%02X ",rec[i]);
dataBlock1 += temp;
}
std::ofstream out(file);
I am getting this error can not convert parameter 1 from wchar * to const char*
on while using the below write function to write hexa datas to a file
out.write(myReader.dataBlock1.GetBuffer(),myReader.dataBlock1.GetLength());
how can we convert wchar_* to const char* to make the write function work.
Thanks.
You can use the wcstombs function, reference here.
Windows has a set of classes and functions that take wchar_t, which is text stored as UTF-16, and char, which is text stored in your ANSI character set. If you have pointer to wchar_t you either need to use an appropriate class or function that accepts wchar_t, or you need to convert the data to your ANSI character set.
In this case, you want the wchar_t variant of ofstream, wofstream.