Hello is there any way to convert a JUCE string to a std::string? I cannot seem to figure out how to do it in any way.
string GetGate() { return (to_string(Gate.char)); };
juce::String Gate = "A1";
Seems pretty simple:
Gate.toStdString()
String::toStdString
According to the JUCE manual, there is the method std::string juce::String::toStdString() const.
Related
I am using the casablanca C++ Rest library to make HTTP requests.
The problem is that this gives a utility::string_t string as output and I can't quite find any way to convert this to a classic std::string. Any ideas?
client.request(methods::GET).then([](http_response response)
{
if(response.status_code() == status_codes::OK)
{
string_t s = response.extract_string().get();
}
});
Depending on what platform you are compiling for, the utility::string_t type will be typedef'd to either std::wstring (on Windows) or std::string (on Linux/OSX).
To get a classic utf-8 std::string regardless of platform, take a look at utility::conversions::to_utf8string.
reference documentation
If you see the documentation for C++ REST SDK from github, you'll find a typedef
C++ Rest SDK - utility Namespace Reference
typedef std::string string_t;
So no need to convert it. Both are same types.
On Windows Phone 8.1 there is this define:
typedef std::wstring string_t;
I used this:
string_t toStringT = U("sample");
std::string fromStringT(toStringT.begin(), toStringT.end());
or:
std::string fromStringT(conversions::to_utf8string(toStringT));
I want to add a string into C++ rest sdk json object. I tried
json::value obj;
obj[L"Login"] = json::value::string();
But the problem is this adds only strings as U("str") and not const std::string.
So I can add as
obj[L"Login"] = json::value::string(U("Login"));
but not do as follows:
json::value obj;
string Login= "login";
obj[L"Login"] = json::value::string(Login);
I am using C++ rest sdk 2.8.0.
Try the following type conversion:
json::value obj;
string Login= "login";
std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
std::wstring uLogin = converter.from_bytes(Login);
obj[L"Login"] = json::value::string(uLogin);
Better switch all your
std::string
to
std::wstring
objects.
cppRest only works with wide strings, so you will end up converting all over the place...
I think my problem is simple to solve, but due to my lack of knowledge, I can't find the answer.
I have this structure :
struct variableOutil {
std::string Nom;
float Valeur;
std::string Module;
std::string Unite;
std::string Format;
std::string Accesibilite;
std::string Description ;
};
And the problem come from here :
struct variableOutil ToleranceTensionAvion;
ToleranceTensionAvion.Nom = "ToleranceTensionAvion";
QTableWidgetItem* newItem = new QTableWidgetItem();
newItem->setText(ToleranceTensionAvion.Nom);
this->ui->variableTableWidget->setItem(1,0,newItem);
I've got this error :
No matching function for call to 'QTableWidgetItem::setText(std::string&)
The problem is that setText need a const QString &text and tell me that I put in parameter a std::string& , I don't understand why does the type don't match, and what's the difference, after all, this is a simple String.
Thank you.
You should convert std::string to QString by :
newItem->setText(QString::fromStdString(ToleranceTensionAvion.Nom));
Or
newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());
Try:
newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());
QString allows only Unicode.
std::string just stores the bytes and does not work with encodings. The best way to store your texts would probably be UTF-8 encoding.
They are completely different classes, and std::stringcan not be used in place of QString. You can build QString fromstd::string and pass it.
QString s =QString::fromStdString(ToleranceTensionAvion.Nom);
newItem->setText(s);
just use c._str() on your std::string and it will work for anything that expects a QString
I'm currently working on a larger project, where the "logic" is implemented in standard C++ with all strings being handled with std::wstring and the UI part is implemented using Qt and thus necessarily QString (Bonus question: is this true?).
What is the best way to connect those two worlds?
I know I can use something like
std::wstring wideString;
QString qtString = QString::fromStdWString(wideString);
but I'm wondering if there is a nicer way with less typing involved. A user defined operator came to my mind, but I'm not experienced enough to tackle that on my own.
Would be glad if anyone could point me in the right direction.
It's a good idea to use QString::fromStdWString but (!!!) if Qt was compiled with exactly the same STL headers as your project. If not - you can get a lot of fun, catching a bug.
If you don't sure that both STL headers are the same use QString::fromWCharArray:
std::wstring wideString;
QString qtString = QString::fromWCharArray( wideString.c_str() );
Update: answering to #juzzlin:
Lets imagine that Qt was build with the STL containing the following std::wstring:
class wstring { // I know, that there's no such class, but I'm shure you'll understand what I want to say
wchar_t * m_ptr;
size_t m_length;
...
};
and you have the STL containing the following std::wstring:
class wstring {
wchar_t * m_ptr;
wchar_t * m_the_end;
...
};
If you'll give your std::wstring to Qt, it will interpret m_the_end pointer as the length of the string, and
you can get a lot of fun, catching a bug
I think a user defined conversion is what you're looking for, and from what I can gather, it should look something like this:
class foo
{
public:
operator QString(std::wstring& ws)
{
return QString::fromStdWString(ws);
}
}
maybe make a inline function QString toQString(std::wstring string) to make it 'less to type' ...
but to be honest ... thats not the big effort at all to write it from time to time ;)
soo long zai
So I've been using SFML lately and I was wondering how I could "add" to sf::String.
For example:
sf::String exampleText;
exampleText.SetText("I say: ");
exampleText += "Blah";
Result: "I say: Blah"
sf::string doesn't offer an append method which makes sense as it's intended to be a class for the graphical display of text rather than a traditional string class.
So you have to perform your string manipulation/append operations behind the scenes using your usual char array/string/stringstream classes and then call sf::string::SetText to update it.
sf::String exampleText;
exampleText.SetText("I say: ");
std::wstring toAppend(L"Blah");
exampleText.SetText(exampleText.GetUnicodeText() + toAppend);
Try that. I have never used sf though.
GetUnicodeText returns std::wstring. And by using the + it may work. Try it.
OR (now that I saw the sf docs better)
exampleText.SetText(exampleText.GetText() + "Blah");
GetText() returns std::string
SetText() Accepts both wstring and string