|
You last visited: Today at 14:55
Advertisement
String 2 LPCWSTR ?!
Discussion on String 2 LPCWSTR ?! within the C/C++ forum part of the Coders Den category.
09/08/2016, 12:29
|
#1
|
elite*gold: 0
Join Date: Sep 2016
Posts: 1
Received Thanks: 0
|
String 2 LPCWSTR ?!
Hello guys im searching for method to convert a std::string 2 a lpcwstr
google just refers me in a circle hope someone of you can give a correct clear anwser  Error say : no suitable constructor exists to convert from "const char*" to std::basic_string....
ty very mutch for trying to help out people
|
|
|
09/08/2016, 12:43
|
#2
|
elite*gold: 966
Join Date: Apr 2010
Posts: 1,105
Received Thanks: 681
|
Just convert your std::string to an std::wstring and provide the method you're about to call with .c_str(). However, that error is a bit odd for what you're trying, so if you got problems you'd best include the code causing them.
Code:
#include <string>
...
std::string x("This is my string.");
std::wstring y;
for (char c : x) y.push_back(c);
ImportantMethodTakingLPCWSTR(y.c_str());
...
There are other ways, but this is pretty straightforward I suppose. You could also manage the memory of the LPCWSTR yourself instead of letting this do std::wstring. As you're a beginner as it seems I wouldn't recommend it though.
With best regards
Jeoni
|
|
|
09/11/2016, 01:25
|
#3
|
elite*gold: 155
Join Date: Aug 2009
Posts: 628
Received Thanks: 153
|
You can just use  to convert between std::string and std::wstring using your encoding of choice.
Little sample using UTF-8:
Code:
#include <codecvt>
#include <locale>
int main()
{
std::string stringToConvert = "Hello World";
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> utf8_conv;
std::wstring converted = utf8_conv.from_bytes(stringToConvert);
}
As LPWCSTR is just a const wchar_t* you can then use converted.c_str() to get your LPWCSTR value out of converted.
Hope this suits your needs!
|
|
|
09/17/2016, 18:06
|
#4
|
elite*gold: 0
Join Date: Sep 2016
Posts: 25
Received Thanks: 25
|
Hey. First of all std::string is a class which is meant for easier handling of a string.
This means there is no conversion to a built-in type.
The cleanest method is described above by Terreox. It's always a good practice too use the STL.
A more low-level attempt would be to use MultiByteToWideChar or a function implemented in CRT, mbstowcs to convert a given character-sequence from utf8 to utf16 (unicode) and then construct a string object.
|
|
|
 |
Similar Threads
|
[C#] String ersetzen in grossem string
10/09/2015 - .NET Languages - 0 Replies
Moin alle zusammen
Ich schreibe im Moment eine kleine WPF Applikation um das Updaten von Kundenprojekt in unserer Firma zu vereinfachen. Dazu habe ich eine Liste von SQL-Skripts welche ausgeführt werden müssen. Ich lesen den Inhalt des Skripts in eine string variable. Diese Skripts enthalten jeweils eine Zeile 'USE '. Nun muss ich das 'KORREKTE_DATENBANK_EINTRAGEN' durch den DB Namen ersetzen.
Folgenden Code habe ich probiert aber es scheint nichts zu ändern:
string content =...
|
LPCWSTR Konvertierung
04/06/2014 - C/C++ - 4 Replies
Moin,
char space;
>> Konvertierung des Parameters 2 von 'char ' in 'LPCWSTR' nicht möglich
Der zweite Parameter ist der space:
pFont->DrawText(NULL, space, );
Hat dafür wer eine Lösung?
|
[Visual Basic] [Problem] String auslesen/String zufällig wählen
05/06/2012 - General Coding - 4 Replies
Code:
#include <GUIConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Name Generator", 236, 299, 815, 246)
$Input1 = GUICtrlCreateInput("Username", 24, 72, 185, 21)
$Input2 = GUICtrlCreateInput("Username", 24, 104, 185, 21)
$Input3 = GUICtrlCreateInput("Username", 24, 136, 185, 21)
$Input4 = GUICtrlCreateInput("Username", 24, 168, 185, 21)
$Input5 = GUICtrlCreateInput("Username", 24, 200, 185, 21)
|
[VB08]String in String mit mehreren Funden
08/08/2011 - .NET Languages - 6 Replies
Hey,
bin gerade auf ein Problem gestoßen, an dem ich mir seit 3 Stunden die Zähne ausbeiße.
Ich will eine Funktion schreiben, die der _StringBetween Funktion von AutoIt gleich ist. _StringBetween gibt in einem Array alle Strings zwischen zwei SubStrings und dem ganzen String aus. Die Ausgabe bei
_StringBetween("<h1>test1</h1>&l t;h1>test2</h1>", "<h1>", "</h1>") wäre also idealer Weiße ein Array (x = "test1", x = "test2")...
da man in VB08 kein Array returnen kann, komme ich aber einfach...
|
All times are GMT +1. The time now is 14:56.
|
|